home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / lib / MANUAL < prev    next >
Encoding:
Text File  |  1998-04-15  |  446.8 KB  |  9,657 lines

  1.  
  2.                     University of Amsterdam
  3.  
  4.                        Dept.  of Social
  5.                       Science Informatics
  6.  
  7.                              (SWI)
  8.                      Roeterstraat 15, 1018
  9.                          WB  Amsterdam
  10.                         The Netherlands
  11.                     Tel.  (+31) 20 5256121
  12.  
  13.                          SSWWII--PPrroolloogg 22..99
  14.  
  15.                         RReeffeerreennccee MMaannuuaall
  16.  
  17.              _U_p_d_a_t_e_d _f_o_r _v_e_r_s_i_o_n _2_._9_._9_, _M_a_r_c_h _1_9_9_8
  18.  
  19.                          _J_a_n _W_i_e_l_e_m_a_k_e_r
  20.  
  21.                        jan@swi.psy.uva.nl
  22.  
  23. SWI-Prolog  is  a Prolog  implementation based  on a  subset of
  24. the  WAM (Warren Abstract Machine  [Warren, 1983]).  SWI-Prolog
  25. has  been  designed and  implemented  such that  it  can easily
  26. be  modified  for experiments  with logic  programming  and the
  27. relation   between  logic  programming  and  other  programming
  28. paradigms   (such  as  the  object  oriented  XPCE  environment
  29. [Anjewierden & Wielemaker, 1989]).   SWI-Prolog has  a rich set
  30. of  built-in predicates and reasonable performance, which makes
  31. it  possible to  develop substantial applications  in it.   The
  32. current  version offers a module system, garbage collection and
  33. an interface to the C language.
  34.  
  35. This  document gives an overview of the features, system limits
  36. and built-in predicates.
  37.  
  38. Copyright Oc 1990--1998, University of Amsterdam
  39.  
  40.  
  41. CChhaapptteerr 11..  IINNTTRROODDUUCCTTIIOONN
  42.  
  43.  
  44. 11..11 SSWWII--PPrroolloogg
  45.  
  46. SWI-Prolog  has   been  designed  and  implemented   to  get  a   Prolog
  47. implementation which can be used for experiments  with logic programming
  48. and  the  relation  to other  programming  paradigms.     The  intention
  49. was  to  build  a Prolog  environment  which  offers  enough  power  and
  50. flexibility to  write substantial applications,  but is  straightforward
  51. enough to  be modified for experiments  with debugging, optimisation  or
  52. the introduction of  non-standard data types.  Performance  optimisation
  53. is  limited due  to the  main objectives:    portability (SWI-Prolog  is
  54. entirely written in C and Prolog) and modifiability.
  55.  
  56. SWI-Prolog  is based  on  a very  restricted  form  of the  WAM  (Warren
  57. Abstract Machine) described  in [Bowen & Byrd, 1983] which defines  only
  58. 7  instructions.   Prolog  can  easily be  compiled into  this  language
  59. and the  abstract machine code  is easily  decompiled back into  Prolog.
  60. As  it is  also  possible to  wire a  standard  4-port debugger  in  the
  61. WAM interpreter  there is  no need  for a  distinction between  compiled
  62. and interpreted  code.   Besides  simplifying the design  of the  Prolog
  63. system  itself this  approach has  advantages  for program  development:
  64. the  compiler is  simple and  fast, the  user  does not  have to  decide
  65. in  advance whether  debugging  is required  and  the system  only  runs
  66. slightly  slower when  in debug  mode.   The  price we  have  to pay  is
  67. some  performance degradation  (taking  out the  debugger from  the  WAM
  68. interpreter improves performance  by about 20%) and somewhat  additional
  69. memory usage to help the decompiler and debugger.
  70.  
  71. SWI-Prolog  extends  the  minimal  set  of   instructions  described  in
  72. [Bowen & Byrd, 1983]  to improve  performance.    While  extending  this
  73. set care  has been  taken to  maintain the  advantages of  decompilation
  74. and  tracing of  compiled  code.    The extensions  include  specialised
  75. instructions  for unification,  predicate  invocation,  some  frequently
  76. used built-in  predicates, arithmetic, and  control (;//22, |//22),  if-then
  77. (->//22) and not (\+//11).
  78.  
  79. This manual  does not  describe the  full syntax and  semantics of  SWI-
  80. Prolog, nor how  one should write a program  in Prolog.  These  subjects
  81. have been described extensively in the literature.   See [Bratko, 1986],
  82. [Sterling & Shapiro, 1986],   and   [Clocksin & Melish, 1987].       For
  83. more  advanced  Prolog   material  see  [OKeefe, 1990].     Syntax   and
  84. standard  operator declarations  confirm  to the  `Edinburgh  standard'.
  85. Most  built  in  predicates  are  compatible  with  those  described  in
  86. [Clocksin & Melish, 1987].      SWI-Prolog  also  offers  a  number   of
  87. primitive  predicates compatible  with  Quintus Prolog  [Qui, 1997]  and
  88. BIM_Prolog [BIM, 1989].
  89.  
  90. ISO  compliant  predicates  are based  on  ``Prolog:    The  Standard'',
  91. [Deransart _e_t _a_l_., 1996].
  92.  
  93.  
  94. 11..22 SSttaattuuss
  95.  
  96. This manual describes  version 2.9 of SWI-Prolog.   SWI-Prolog has  been
  97. used  now for  several years.    The application  range includes  Prolog
  98. course  material,  meta-interpreters,  simulation  of  parallel  Prolog,
  99. learning systems, natural language processing and  two large workbenches
  100. for knowledge engineering.   Although we experienced rather  obvious and
  101. critical bugs can remain unnoticed for a remarkable long  period, we can
  102. assume the basic Prolog  system is fairly stable.  Bugs can  be expected
  103. in infrequently used builtin predicates.
  104.  
  105. Some bugs are known to  the author.  They are described as  footnotes in
  106. this manual.
  107.  
  108.  
  109. 11..33 SShhoouulldd yyoouu bbee UUssiinngg SSWWII--PPrroolloogg??
  110.  
  111. There are a number of reasons why you better  choose a commercial Prolog
  112. system, or another academic product:
  113.  
  114.   o _S_W_I_-_P_r_o_l_o_g _i_s _n_o_t _s_u_p_p_o_r_t_e_d
  115.     Although  I usually fix bugs shortly  after a bug report arrives,  I
  116.     cannot  promise anything.   Now that the  sources are provided,  you
  117.     can always dig into them yourself.
  118.  
  119.   o _M_e_m_o_r_y _r_e_q_u_i_r_e_m_e_n_t_s _a_n_d _p_e_r_f_o_r_m_a_n_c_e _a_r_e _y_o_u_r _f_i_r_s_t _c_o_n_c_e_r_n_s
  120.     A  number  of  commercial compilers  are  more  keen on  memory  and
  121.     performance  than SWI-Prolog.   I do not  wish to sacrifice some  of
  122.     the  nice features of the system, nor its portability to  compete on
  123.     raw performance.
  124.  
  125.   o _Y_o_u _n_e_e_d _f_e_a_t_u_r_e_s _n_o_t _o_f_f_e_r_e_d _b_y _S_W_I_-_P_r_o_l_o_g
  126.     In  this case you  may wish to  give me suggestions for  extensions.
  127.     If  you  have great  plans, please  contact me  (you  might have  to
  128.     implement them yourself however).
  129.  
  130. On the other hand, SWI-Prolog offers some nice facilities:
  131.  
  132.   o _N_i_c_e _e_n_v_i_r_o_n_m_e_n_t
  133.     This includes `Do  What I Mean', automatic completion of atom names,
  134.     history mechanism and  a tracer that operates on single key-strokes.
  135.     Interfaces  to standard  Unix editors  are  provided, as  well as  a
  136.     facility to maintain programs (see mmaakkee//00).
  137.  
  138.   o _V_e_r_y _f_a_s_t _c_o_m_p_i_l_e_r
  139.     The  compiler handles  about  100K bytes  per second  on a  SPARC-II
  140.     processor.
  141.  
  142.   o _T_r_a_n_s_p_a_r_e_n_t _c_o_m_p_i_l_e_d _c_o_d_e
  143.     SWI-Prolog  compiled code can be  treated just as interpreted  code:
  144.     you  can list  it,  trace it,  assert to  or retract  from it,  etc.
  145.     This  implies you do not have to decide beforehand whether  a module
  146.     should  be loaded for debugging or  not.  Also, performance  is much
  147.     better than the performance of most interpreters.
  148.  
  149.   o _P_r_o_f_i_l_i_n_g
  150.     SWI-Prolog offers tools  for performance analysis, which can be very
  151.     useful  to optimise  programs.   Unless you  are very familiar  with
  152.     Prolog  and Prolog  performance  considerations this  might be  more
  153.     helpful than a better compiler without these facilities.
  154.  
  155.   o _F_l_e_x_i_b_i_l_i_t_y
  156.     SWI-Prolog  allows  for   easy  and  flexible  integration  with  C,
  157.     both  Prolog calling  C functions  as C  calling Prolog  predicates.
  158.     SWI-Prolog  is provided  in source  form,  which implies  SWI-Prolog
  159.     can  be linked in  with another package.   Command line options  and
  160.     predicates  to obtain information from the system and  feedback into
  161.     the system are provided.
  162.  
  163.   o _I_n_t_e_g_r_a_t_i_o_n _w_i_t_h _X_P_C_E
  164.     SWI-Prolog   offers  a   tight  integration  to   the  Object   Ori-
  165.     ented   Package  for   User  Interface   Development,  called   XPCE
  166.     [Anjewierden & Wielemaker, 1989].    XPCE  allows you  to  implement
  167.     graphical  user  interfaces  that are  source-code  compatible  over
  168.     Unix/X11 and Win32 (Windows 95 and NT).
  169.  
  170.  
  171. 11..44 TThhee XXPPCCEE GGUUII ssyysstteemm ffoorr PPrroolloogg
  172.  
  173. The  XPCE GUI  system  for dynamically  typed  languages has  been  with
  174. SWI-Prolog for  a long time.   It is  developed by Anjo Anjewierden  and
  175. Jan  Wielemaker from  the department  of SWI,  University of  Amsterdam.
  176. It  aims at  a  high-productive  development environment  for  graphical
  177. applications based on Prolog.
  178.  
  179. Object  oriented  technology has  proven  to  be a  suitable  model  for
  180. implementing GUIs, which  typically deal with things Prolog is  not very
  181. good  at:   event-driven  control  and global  state.    With  XPCE,  we
  182. designed  a system  that has  similar characteristics  that make  Prolog
  183. such  a powerful  tool:   dynamic typing,  meta-programming and  dynamic
  184. modification of the running system.
  185.  
  186. XPCE is  an object-system written  in the C-language.   It provides  for
  187. the implementation of methods  in multiple languages.  New  XPCE classes
  188. may be defined from Prolog using a simple, natural syntax.   The body of
  189. the method is executed  by Prolog itself, providing a  natural interface
  190. between the two systems.  Below is a very simple class definition.
  191.  
  192. :- pce_begin_class(prolog_lister, frame,
  193.                    "List Prolog predicates").
  194.  
  195. initialise(Self) :->
  196.         "As the C++ constructor"::
  197.         send(Self, send_super, initialise, 'Prolog Lister'),
  198.         send(Self, append, new(D, dialog)),
  199.         send(D, append,
  200.              text_item(predicate, message(Self, list, @arg1))),
  201.         send(new(view), below, D).
  202.  
  203. list(Self, From:name) :->
  204.         "List predicates from specification"::
  205.         (   term_to_atom(Term, From)
  206.         ->  get(Self, member, view, V),
  207.             pce_open(V, write, Fd),
  208.             set_output(Fd),
  209.             listing(Term),
  210.             close(Fd)
  211.         ;   send(Self, report, error, 'Syntax error')
  212.         ).
  213.  
  214. :- pce_end_class.
  215.  
  216. test :- send(new(prolog_lister), open).
  217.  
  218. The  165  built-in   classes  deal  with  the  meta-environment,   data-
  219. representation  and---of  course---graphics.     The   graphics  classes
  220. concentrate on direct-manipulation of diagrammatic representations.
  221.  
  222. AAvvaaiillaabbiilliittyy.. XPCE  runs  on  most  Unixtm  platforms,  Windows  95  and
  223. Windows NT. It has been connected to SWI-Prolog, SICStustm and Quintustm
  224. Prolog as well  as some Lisp dialects and  C++.  The Quintus version  is
  225. commercially distributed and supported as ProWindows-3tm.
  226.  
  227. IInnffoo.. further        information        is         available        from
  228. http://www.swi.psy.uva.nl/projects/xpce/home.html   or  by   E-mail   to
  229. xpce-request@swi.psy.uva.nl.   There is  a demo  version for Windows  95
  230. and  NT  in  ftp://swi.psy.uva.nl/pub/xpce/Windows/bin/,   and  one  for
  231. the  i386/Linux  system in  ftp://swi.psy.uva.nl/pub/xpce/linux.     For
  232. information  on  ProWindows-3,  please  see  http://www.aiil.co.uk,   or
  233. contact sales@aiil.co.uk.
  234.  
  235.  
  236. 11..55 VVeerrssiioonn 11..55 RReelleeaassee NNootteess
  237.  
  238. There are not many  changes between version 1.4 and 1.5.   The C-sources
  239. have been  cleaned and  comments have been  updated.   The stack  memory
  240. management based on  using the MMU has been  changed to run on a  number
  241. of System-V  Unix systems offering  shared memory.   Handling dates  has
  242. been  changed.   All  functions  handling dates  now return  a  floating
  243. point number,  expressing the  time in  seconds since  January 1,  1970.
  244. A predicate  ccoonnvveerrtt__ttiimmee//88 is available to  get the  year, month,  etc.
  245. The predicate  ttiimmee//66 has  been deleted.   ggeett__ttiimmee//11 and ccoonnvveerrtt__ttiimmee//88
  246. together do the same.
  247.  
  248. From version 1.5, the system is distributed in source  form, rather than
  249. in object  form as used with  previous releases.   This allows users  to
  250. port SWI-Prolog  to new  machines, extend and  improve the  system.   If
  251. you want  your changes to  be incorporated in  the next release,  please
  252. indicate  all changes  using  a C-preprocessor  flag and  send  complete
  253. source files  back to  me.   Difference  listings are  of no  use, as  I
  254. generally won't have exactly the same version around.
  255.  
  256.  
  257. 11..66 VVeerrssiioonn 11..66 RReelleeaassee NNootteess
  258.  
  259. Version  1.6 is  completely  compatible with  version  1.5.    Some  new
  260. features have  been added,  the system  has been ported  to various  new
  261. platforms and  there is  a provisional  interface to  GNU Emacs.    This
  262. interface will be improved and documented later.
  263.  
  264. The WAM  virtual-machine interpreter  has been modified  to use  GCC-2's
  265. support for threaded code.
  266.  
  267. From version 1.6,  the sources are now  versioned using the CVS  version
  268. control system.
  269.  
  270.  
  271. 11..77 VVeerrssiioonn 11..77 RReelleeaassee NNootteess
  272.  
  273. Version  1.7  integrates the  GNU-readline  library,  offering  powerful
  274. history and command-line editing both using Emacs and vi key-bindings.
  275.  
  276.  
  277. 11..88 VVeerrssiioonn 11..88 RReelleeaassee NNootteess
  278.  
  279. Version  1.8 offers  a stack-shifter  to  provide dynamically  expanding
  280. stacks  on machines  that  do  not offer  operating-system  support  for
  281. implementing dynamic stacks.
  282.  
  283.  
  284. 11..99 VVeerrssiioonn 11..99 RReelleeaassee NNootteess
  285.  
  286. Version  1.9  offers  better portability  including  an  MS-Windows  3.1
  287. version.  Changes to the Prolog system include:
  288.  
  289.   o _R_e_d_e_f_i_n_i_t_i_o_n _o_f _s_y_s_t_e_m _p_r_e_d_i_c_a_t_e_s
  290.     Redefinition  of system  predicates  was allowed  silently in  older
  291.     versions.    Version 1.9  only allows it  if the  new definition  is
  292.     headed by a :- rreeddeeffiinnee__ssyysstteemm__pprreeddiiccaattee//11directive.
  293.  
  294.   o _`_A_n_s_w_e_r_' _r_e_u_s_e
  295.     The  toplevel maintains  a table  of bindings  returned by  toplevel
  296.     goals  and  allows for  reuse  of these  bindings by  prefixing  the
  297.     variables with the $ sign.  See section 2.5.
  298.  
  299.   o _B_e_t_t_e_r _s_o_u_r_c_e _c_o_d_e _a_d_m_i_n_i_s_t_r_a_t_i_o_n
  300.     Allows  for proper updating of multifile predicates and  finding the
  301.     sources of individual clauses.
  302.  
  303.  
  304. 11..1100 VVeerrssiioonn 22..00 RReelleeaassee NNootteess
  305.  
  306. Version 2.0 is  first of all a freeze  of all the features added to  the
  307. various 1.9.x releases.  Version 2.0.6 for PC has  moved from the WATCOM
  308. C  32-bit windows  extender to  Windows NT  and runs  under Windows  3.1
  309. using the Win32s NT emulator.
  310.  
  311. New features offered:
  312.  
  313.   o _3_2_-_b_i_t _V_i_r_t_u_a_l _M_a_c_h_i_n_e
  314.     Removes various limits and improves performance.
  315.  
  316.   o _I_n_l_i_n_e _f_o_r_e_i_g_n _f_u_n_c_t_i_o_n_s
  317.     `Simple'  foreign predicates no  longer build a Prolog  stack-frame,
  318.     but are directly  called from the VM. Notably provides a speedup for
  319.     the test predicates such as vvaarr//11, etc.
  320.  
  321.   o _V_a_r_i_o_u_s _c_o_m_p_a_t_i_b_i_l_i_t_y _i_m_p_r_o_v_e_m_e_n_t_s
  322.  
  323.   o _S_t_r_e_a_m _b_a_s_e_d _I_/_O _l_i_b_r_a_r_y
  324.     All  SWI-Prolog's I/O is now  handled by the stream-package  defined
  325.     in  the foreign include file SWI-Stream.h.   Physical I/O of  Prolog
  326.     streams  may be  redefined through the  foreign language  interface,
  327.     facilitating much simpler integration in window environments.
  328.  
  329. Version 2.0.6 offers a few incompatibilities:
  330.  
  331.   o rreettrraaccttaallll//11
  332.     In previous releases, the definition of rreettrraaccttaallll//11 was:
  333.  
  334.     retractall(Term) :-
  335.             retract(Term),
  336.             fail.
  337.     retractall(_).
  338.  
  339.     As   from  version   2.0.6,   rreettrraaccttaallll//11  is   implemented  as   a
  340.     deterministic foreign predicate  compatible with Quintus Prolog.  It
  341.     behaves as:
  342.  
  343.     retractall(Head) :-
  344.             retract(Head),
  345.             fail.
  346.     retractall(Head) :-
  347.             retract((Head :- _)),
  348.             fail.
  349.     retractall(_).
  350.  
  351.     I.e.  the  definition  behaves  the same  when  handling  predicates
  352.     consisting  of  facts.     Clauses with  a  non-true  body  will  be
  353.     retracted if their head matches.
  354.  
  355.   o _F_o_r_e_i_g_n _i_n_t_e_r_f_a_c_e _t_y_p_e_s
  356.     All  foreign interface types now  have names ending in  _t to  lessen
  357.     the  chance for conflicts.   term, atomic,  functor and module  have
  358.     #define's for backward compatibility.
  359.  
  360.   o PPLL__rreeggiisstteerr__ffoorreeiiggnn(())
  361.     The  attributes is now  a bitwise or  of the attribute flags  rather
  362.     than  a 0 terminated list.  This has no consequences  for predicates
  363.     that  have no attributes (99%  of them), while predicates with  just
  364.     one  attribute will generate a  compiler warning, but work  properly
  365.     otherwise.     Predicates with  more  than  one attributes  must  be
  366.     changed.
  367.  
  368.   o PL_dispatch_events
  369.     This  pointer  is replaced  by PPLL__ddiissppaattcchh__hhooookk(()).    A function  was
  370.     necessary for the Win32 .DLL interface.
  371.  
  372.  
  373. 11..1111 VVeerrssiioonn 22..11 RReelleeaassee NNootteess
  374.  
  375. In addition  to several  bug fixes,  the 2.1 versions  provide some  new
  376. features:
  377.  
  378.   o sseettaarrgg//33
  379.     A new  predicate sseettaarrgg//33 for extra-logical (destructive) assignment
  380.     to arguments of terms is provided.
  381.  
  382.   o _M_o_d_i_f_i_e_d kkeeyyssoorrtt//22
  383.     kkeeyyssoorrtt//22  is now stable with regard to multiple values on  the same
  384.     key.  Makes this predicate compatible with SICStus and Quintus.
  385.  
  386.   o _M_o_d_i_f_i_e_d _g_r_a_m_m_a_r _r_u_l_e _e_x_p_a_n_s_i_o_n
  387.     DCG  translation of  free variables  now calls  pphhrraassee//33, which  has
  388.     been  changed slightly to deal  with `un-parsing'.  Modification  is
  389.     probably  not complete,  but it fixes  some problems encountered  by
  390.     Michael B"ohlen.
  391.  
  392.   o _E_x_c_e_p_t_i_o_n _h_a_n_d_l_i_n_g
  393.     The  top of the runtime  stack are automatically dumped on  floating
  394.     point exceptions.
  395.  
  396.   o _F_o_r_e_i_g_n _i_n_t_e_r_f_a_c_e
  397.     Added   facilities  to   allow   for  embedding   SWI-Prolog  in   C
  398.     applications.
  399.  
  400.  
  401. 11..1122 VVeerrssiioonn 22..55 RReelleeaassee NNootteess
  402.  
  403. Version 2.5  is an  intermediate release on  the path  from 2.1 to  3.0.
  404. All  changes  are to  the  foreign-language  interface,  both  to  user-
  405. and  system predicates  implemented  in the  C-language.    The  aim  is
  406. twofold.   First of all  to make garbage-collection and  stack-expansion
  407. (stack-shifts)  possible  while  foreign  code  is  active  without  the
  408. C-programmer having  to worry  about locking  and unlocking  C-variables
  409. pointing  to Prolog  terms.    The new  approach is  closely  compatible
  410. to the  Quintus and  SICStus Prolog  foreign interface  using the  +term
  411. argument specification (see their respective manuals).   This allows for
  412. writing foreign  interfaces that  are easily portable  over these  three
  413. Prolog platforms.
  414.  
  415. According  to the  current plan,  ISO compliant  exception handling  and
  416. hooks for source-code debugging will be added before the  system will be
  417. called 3.0.
  418.  
  419. Apart from  various bug fixes  listed in the  Changelog file, these  are
  420. the main changes since 2.1.0:
  421.  
  422.   o _I_S_O _c_o_m_p_a_t_i_b_i_l_i_t_y
  423.     Many   ISO  compatibility  features   have  been  added:     ooppeenn//44,
  424.     arithmetic functions, syntax, etc.
  425.  
  426.   o _W_I_N_3_2
  427.     Many  fixes for the Win32 (NT,  '95 and win32s) platforms.   Notably
  428.     many  problems related  to pathnames  and a problem  in the  garbage
  429.     collector.
  430.  
  431.   o _P_e_r_f_o_r_m_a_n_c_e
  432.     Many  changes to  the clause  indexing system:   added  hash-tables,
  433.     lazy computation of the index information, etc.
  434.  
  435.   o _P_o_r_t_a_b_l_e _s_a_v_e_d_-_s_t_a_t_e_s
  436.     The   predicate  qqssaavvee__pprrooggrraamm//[[11,,22]] allows  for  the   creating  of
  437.     machine independent saved-states that load very quickly.
  438.  
  439.  
  440. 11..1133 VVeerrssiioonn 22..66 RReelleeaassee NNootteess
  441.  
  442. Version 2.6  provides a stable implementation  of the features added  in
  443. the 2.5.x  releases, but  at the same  time implements  a number of  new
  444. features that may have impact on the system stability.
  445.  
  446.   o _3_2_-_b_i_t _i_n_t_e_g_e_r _a_n_d _d_o_u_b_l_e _f_l_o_a_t _a_r_i_t_h_m_e_t_i_c
  447.     The  biggest change is the  support for full 32-bit signed  integers
  448.     and  raw  machine-format  double precision  floats.    The  internal
  449.     data  representation as well as  the arithmetic instruction set  and
  450.     interface to the arithmetic functions has been changed for this.
  451.  
  452.   o _E_m_b_e_d_d_i_n_g _f_o_r _W_i_n_3_2 _a_p_p_l_i_c_a_t_i_o_n_s
  453.     The  Win32 version has been reorganised.   The Prolog kernel is  now
  454.     implemented  as Win32  DLL that may  be embedded in  C-applications.
  455.     Two front ends  are provided, one for window-based operation and one
  456.     to run as a Win32 console application.
  457.  
  458.   o _C_r_e_a_t_i_n_g _s_t_a_n_d_-_a_l_o_n_e _e_x_e_c_u_t_a_b_l_e_s
  459.     Version  2.6.0 can create  stand-alone executables by attaching  the
  460.     saved-state to the emulator.  See qqssaavvee__pprrooggrraamm//22.
  461.  
  462.  
  463. 11..1144 VVeerrssiioonn 22..77 RReelleeaassee NNootteess
  464.  
  465. Version 2.7  reorganises the  entire data-representation  of the  Prolog
  466. data  itself.   The  aim is  to remove  most of  the  assumption on  the
  467. machine's memory  layout to  improve portability in  general and  enable
  468. embedding on  systems where the memory  layout may depend on  invocation
  469. or on  how the executable is  linked.  The  latter is notably a  problem
  470. on  the Win32  platforms.   Porting  to 64-bit  architectures should  be
  471. feasible now.
  472.  
  473. Furthermore, 2.7 lifts the  limits on arity of predicates and  number of
  474. variables in  a clause considerably and  allow for further expansion  at
  475. minimal cost.
  476.  
  477.  
  478. 11..1155 VVeerrssiioonn 22..88 RReelleeaassee NNootteess
  479.  
  480. data-representation changes of  2.7.x stable.  Version 2.8  exploits the
  481. changes of of 2.7 to  support 64-bit processors like the DEC Alpha.   As
  482. of version 2.8.5, the representation of recorded the  terms has changed,
  483. and  terms  on the  heap  are  now  represented in  a  compiled  format.
  484. SWI-Prolog no longer limits  the use of mmaalllloocc(()) or uses  assumptions on
  485. the addresses returned by this function.
  486.  
  487.  
  488. 11..1166 VVeerrssiioonn 22..99 RReelleeaassee NNootteess
  489.  
  490. Version  2.9  is  the next  step  towards  version  3.0,  improving  ISO
  491. compliance  and introducing  ISO  compliant  exception handling.     New
  492. are ccaattcchh//33, tthhrrooww//11,  aabboolliisshh//11, wwrriittee__tteerrmm//[[22,,33]], wwrriittee__ccaannoonniiccaall//[[11,,22]]
  493. and  the  C-functions PPLL__eexxcceeppttiioonn(()) and  PPLL__tthhrrooww(()).    The  predicates
  494. ddiissppllaayy//[[11,,22]] and ddiissppllaayyqq//[[11,,22]]  have been moved to  library(backcomp),
  495. so old code referring to them will autoload them.
  496.  
  497. The interface  to PPLL__ooppeenn__qquueerryy(()) has changed.   The  _d_e_b_u_g argument  is
  498. replaced  by a  bitwise or'ed  _f_l_a_g_s argument.    The  values FALSE  and
  499. TRUE have their familiar meaning, making old code  using these constants
  500. compatible.   Non-zero values  other than TRUE  (1) will be  interpreted
  501. different.
  502.  
  503.  
  504. 11..1177 AAcckknnoowwlleeddggeemmeennttss
  505.  
  506. Some  small  parts  of  the  Prolog  code  of  SWI-Prolog  are  modified
  507. versions of  the corresponding  Edinburgh C-Prolog code:   grammar  rule
  508. compilation  and wwrriitteeff//22.    Also some  of the  C-code originates  from
  509. C-Prolog:  finding the path of the currently running  executable and the
  510. code underlying  aabbssoolluuttee__ffiillee__nnaammee//22.   Ideas on  programming style  and
  511. techniques originate from  C-Prolog and Richard O'Keefe's _t_h_i_e_f  editor.
  512. An  important  source  of inspiration  are  the  programming  techniques
  513. introduced by Anjo Anjewierden in PCE version 1 and 2.
  514.  
  515. I also  would like to thank  those who had the  fade of using the  early
  516. versions of this system,  suggested extensions or reported bugs.   Among
  517. them are Anjo Anjewierden, Huub Knops, Bob  Wielinga, Wouter Jansweijer,
  518. Luc Peerdeman, Eric Nombden, Frank van Harmelen, Bert Rengel.
  519.  
  520. Martin Jansche  (jansche@novell1.gs.uni-heidelberg.de) has been so  kind
  521. to reorganise the sources for version 2.1.3 of this manual.
  522.  
  523. Horst  von Brand  has been  so  kind to  fix many  typos  in the  2.7.14
  524. manual.  Thanks!
  525.  
  526.  
  527. CChhaapptteerr 22..  OOVVEERRVVIIEEWW
  528.  
  529.  
  530. 22..11 SSttaarrttiinngg SSWWII--PPrroolloogg ffrroomm tthhee UUnniixx SShheellll
  531.  
  532. It  is  advised to  install  SWI-Prolog  as  `pl' in  the  local  binary
  533. directory.    SWI-Prolog can  then be  started from  the  Unix shell  by
  534. typing `pl'.  The system will boot from the  system's default boot file,
  535. perform the  necessary initialisations  and then  enter the  interactive
  536. top level.
  537.  
  538. After  the necessary  system  initialisation  the system  consults  (see
  539. ccoonnssuulltt//11) the  user's initialisation  file.   This initialisation  file
  540. should be named  `.plrc' and reside either  in the current directory  or
  541. in the  user's home directory.   If both  exist the initialisation  file
  542. from the current  directory is loaded.   The name of the  initialisation
  543. file  can be  changed with  the `-f file'  option.    After loading  the
  544. initialisation  file SWI-Prolog  executes  a user  initialisation  goal.
  545. The default goal is  a system predicate that prints the  banner message.
  546. The  default can  be  modified with  the `-g goal'  option.    Next  the
  547. toplevel goal is started.   Default is the interactive Prolog  loop (see
  548. pprroolloogg//00).  The  user can overwrite this default with  the `-t toplevel'
  549. option.
  550.  
  551.  
  552. 22..11..11 CCoommmmaanndd LLiinnee OOppttiioonnss
  553.  
  554. The full set of command line options is given below:
  555.  
  556. --hheellpp
  557.     When  given as  the only  option, it summarises  the most  important
  558.     options.
  559.  
  560. --vv
  561.     When  given as the  only option, it  summarises the version and  the
  562.     architecture identifier.
  563.  
  564. --aarrcchh
  565.     When   given  as  the  only  option,  it  prints   the  architecture
  566.     identifier (see feature(arch, Arch)) and exits.
  567.  
  568. --LL_s_i_z_e_[_k_m_]
  569.     Give  local stack limit (2 Mbytes default).   Note that there  is no
  570.     space  between the size option  and its argument.   By default,  the
  571.     argument  is interpreted in  Kbytes.   Postfixing the argument  with
  572.     m  causes the argument to be  interpreted in Mbytes.  The  following
  573.     example specifies 32 Mbytes local stack.
  574.  
  575.     % pl -L32m
  576.  
  577.     A maximum is  useful to stop buggy programs from claiming all memory
  578.     resources.  -L0 sets the limit to the highest possible value.
  579.  
  580. --GG_s_i_z_e_[_k_m_]
  581.     Give  global  stack limit  (4 Mbytes  default).    See -L  for  more
  582.     details.
  583.  
  584. --TT_s_i_z_e_[_k_m_]
  585.     Give  trail  stack  limit  (4  Mbytes  default).     This  limit  is
  586.     relatively  high because trail-stack overflows are not  often caused
  587.     program bugs.  See -L for more details.
  588.  
  589. --AA_s_i_z_e_[_k_m_]
  590.     Give  argument stack limit (1 Mbytes  default).  The argument  stack
  591.     limits  the  maximum  nesting of  terms  that  can be  compiled  and
  592.     executed.    The  SWI-Prolog  does `last-argument  optimisation'  to
  593.     avoid  many deeply  nested structure  using this stack.    Enlarging
  594.     this  limit is only  necessary in extreme  cases.   See -L for  more
  595.     details.
  596.  
  597. --HH_s_i_z_e_[_k_m_]
  598.     Give  mmaalllloocc(()) heap limit.   The  default is to  raise the limit  as
  599.     high  as possible.  This  option only applies to machines using  the
  600.     mmmmaapp(())  function for allocating the Prolog stacks.  See -L  for more
  601.     details.
  602.  
  603. --cc _f_i_l_e _._._.
  604.     Compile files into an `intermediate code file'.  See section 2.7.
  605.  
  606. --oo _o_u_t_p_u_t
  607.     Used  in combination  with -c  or -b  to determine  output file  for
  608.     compilation.
  609.  
  610. --OO
  611.     Optimised compilation.  See pplleeaassee//33.
  612.  
  613. --ff _f_i_l_e
  614.     Use  _f_i_l_e as  initialisation  file instead  of `.plrc'.    `-f none'
  615.     stops SWI-Prolog from searching for an initialisation file.
  616.  
  617. --FF _s_c_r_i_p_t
  618.     Selects  a startup-script from the  SWI-Prolog home directory.   The
  619.     script-file  is  named <_s_c_r_i_p_t>.rc.    The  default _s_c_r_i_p_t  name  is
  620.     deduced  from  the  executable,  taking the  leading  alphanumerical
  621.     characters  (letters, digits and underscore) from  the program-name.
  622.     -F none stops looking  for a script.  Intended for simple management
  623.     of  slightly  different  versions.    One  could for  example  write
  624.     a  script  iso.rc  and  then select  ISO  compatibility  mode  using
  625.     pl -F iso or make a link from iso-pl to pl.
  626.  
  627. --gg _g_o_a_l
  628.     _G_o_a_l  is executed just before entering the top level.  Default  is a
  629.     predicate  which prints the  welcome message.   The welcome  message
  630.     can  thus be suppressed by  giving -g true.   _g_o_a_l can be a  complex
  631.     term.   In this case quotes  are normally needed to protect  it from
  632.     being expanded by the Unix shell.
  633.  
  634. --tt _g_o_a_l
  635.     Use  _g_o_a_l  as  interactive  toplevel instead  of  the  default  goal
  636.     pprroolloogg//00.    _g_o_a_l  can be  a complex  term.   If  the toplevel  goal
  637.     succeeds  SWI-Prolog exits  with status  0.   If it  fails the  exit
  638.     status  is  1.    This  flag also  determines  the goal  started  by
  639.     bbrreeaakk//00  and aabboorrtt//00.   If you want to  stop the user from  entering
  640.     interactive  mode  start the  application  with `-g goal'  and  give
  641.     `halt' as toplevel.
  642.  
  643. --ttttyy
  644.     Switches  tty  control (using  ioctl(2)) on  (+tty)  or off  (-tty).
  645.     Normally  tty  control is  switched on.    This  default depends  on
  646.     the  installation.    You may  wish  to switch  tty control  off  if
  647.     Prolog  is used  from an  editor such  as Emacs.    If switched  off
  648.     ggeett__ssiinnggllee__cchhaarr//11and the tracer will wait for a return.
  649.  
  650. --xx _b_o_o_t_f_i_l_e
  651.     Boot  from _b_o_o_t_f_i_l_e instead  of the system's default  boot file.   A
  652.     bootfile is a  file resulting from a Prolog compilation using the -b
  653.     or -c option or a program saved using qqssaavvee__pprrooggrraamm//[[11,,22]].
  654.  
  655. --rr _r_e_s_t_o_r_e_f_i_l_e
  656.     Restore  a state  created by ssaavvee__pprrooggrraamm//[[11,,22]] or ssaavvee//[[11,,22]]  using
  657.     the  new-style  saved-states.   Equivalent  to  restore(restorefile)
  658.     from Prolog.
  659.  
  660. --pp _a_l_i_a_s_=_p_a_t_h_1_[_:_p_a_t_h_2 _._._._]
  661.     Define  a path  alias for  file_search_path.    _a_l_i_a_s is  the name  of
  662.     the  alias, _p_a_t_h_1  _._._.   is  a : separated  list of  values for  the
  663.     alias.    A  value is  either a  term of  the  form alias(value)  or
  664.     pathname.    The  computed  aliases are  added to  ffiillee__sseeaarrcchh__ppaatthh//22
  665.     using  aasssseerrttaa//11, so they precede  predefined values for the  alias.
  666.     See  ffiillee__sseeaarrcchh__ppaatthh//22 for  details  on  using  this  file-location
  667.     mechanism.
  668.  
  669. --
  670.     Stops  scanning for more  arguments, so you  can pass arguments  for
  671.     your application after this one.
  672.  
  673. The following options  are for system maintenance.   They are given  for
  674. reference only.
  675.  
  676. --bb _i_n_i_t_f_i_l_e _._._.-c _f_i_l_e _._._.
  677.     Boot  compilation.   _i_n_i_t_f_i_l_e  _._._.   are compiled  by the  C-written
  678.     bootstrap  compiler,  _f_i_l_e  _._._.    by  the normal  Prolog  compiler.
  679.     System maintenance only.
  680.  
  681. --dd _l_e_v_e_l
  682.     Set  debug  level to  _l_e_v_e_l.    Only  has effect  if the  system  is
  683.     compiled with the -DO_DEBUG flag.  System maintenance only.
  684.  
  685.  
  686. 22..22 GGNNUU EEmmaaccss IInntteerrffaaccee
  687.  
  688. A provisional  interface to emacs  has been  included since version  1.6
  689. of  SWI-Prolog.    The interface  is  based  on the  freely  distributed
  690. interface  delivered  with Quintus  Prolog.    When  running  Prolog  as
  691. an  inferior process  under  GNU-Emacs,  there  is support  for  finding
  692. predicate  definitions,  completing  atoms,  finding  the  locations  of
  693. compilation-warnings  and  many  more.    For  details,  see  the  files
  694. pl/lisp/README and pl/lisp/swi-prolog.el.
  695.  
  696.  
  697. 22..33 OOnnlliinnee HHeellpp
  698.  
  699. Online  help  provides a  fast  lookup  and browsing  facility  to  this
  700. manual.   The online  manual can show predicate  definitions as well  as
  701. entire sections of the manual.
  702.  
  703. The online help  is displayed from the file library('MANUAL').  The file
  704. library(helpidx) provides  an index into this  file.   library('MANUAL')
  705. is created  from the LaTeX  sources with a  modified version of  dvitty,
  706. using overstrike  for printing bold text  and underlining for  rendering
  707. italic text.    XPCE is shipped  with library(swi_help), presenting  the
  708. information from  the online help  in a hypertext window.   The  feature
  709. write_help_with_overstrike  controls whether  or  not hheellpp//11  writes  its
  710. output using  overstrike to realise bold  and underlined output or  not.
  711. If this  feature is not  set it  is initialised by  the help library  to
  712. true if  the TERM variable equals  xterm and false  otherwise.  If  this
  713. default does not satisfy you, add the following line to ~/.plrc.
  714.  
  715. :- set_feature(write_help_with_overstrike, true).
  716.  
  717.  
  718. hheellpp
  719.     Equivalent to help(hheellpp//11).
  720.  
  721.  
  722. hheellpp((_+_W_h_a_t))
  723.     Show specified part of the manual.  _W_h_a_t is one of:
  724.  
  725.           <_N_a_m_e>/<_A_r_i_t_y> give help on specified predicate
  726.           <_N_a_m_e>         give  help on  named  predicate with  any
  727.                          arity or  C interface function  with that
  728.                          name
  729.           <_S_e_c_t_i_o_n>      display  specified  section.      section
  730.                          numbers are dash-separated  numbers:  2-3
  731.                          refers  to  section 2.3  of  the  manual.
  732.  
  733.                          Section   numbers  are   obtained   using
  734.                          aapprrooppooss//11.
  735.  
  736.     Examples
  737.  
  738.        ?- help(assert).     give help on predicate assert
  739.        ?- help(3-4).        display section 3.4 of the manual
  740.        ?- help('PL_retry'). give    help   on    interface   function
  741.                             PPLL__rreettrryy(())
  742.  
  743.  
  744. aapprrooppooss((_+_P_a_t_t_e_r_n))
  745.     Display all predicates,  functions and sections that have _P_a_t_t_e_r_n in
  746.     their  name or summary  description.   Lowercase letters in  _P_a_t_t_e_r_n
  747.     also match a corresponding uppercase letter.  Example:
  748.  
  749.         ?- apropos(file).  Display  predicates,  functions and  sec-
  750.                            tions that have  `file' (or `File', etc.)
  751.                            in their summary description.
  752.  
  753.  
  754. eexxppllaaiinn((_+_T_o_E_x_p_l_a_i_n))
  755.     Give an explanation on  the given `object'.  The argument may be any
  756.     Prolog data object.   If the argument is an atom, a term of the form
  757.     _N_a_m_e_/_A_r_i_t_y  or a term  of the  form _M_o_d_u_l_e_:_N_a_m_e_/_A_r_i_t_y, explain  will
  758.     try to explain the predicate as well as possible references to it.
  759.  
  760.  
  761. eexxppllaaiinn((_+_T_o_E_x_p_l_a_i_n_, _-_E_x_p_l_a_n_a_t_i_o_n))
  762.     Unify  _E_x_p_l_a_n_a_t_i_o_n with an explanation for _T_o_E_x_p_l_a_i_n.   Backtracking
  763.     yields further explanations.
  764.  
  765.  
  766. 22..44 QQuueerryy SSuubbssttiittuuttiioonnss
  767.  
  768. SWI-Prolog  offers a  query substitution  mechanism similar  to that  of
  769. Unix csh (csh(1)), called  `history'.  The availability of  this feature
  770. is controlled by sseett__ffeeaattuurree//22, using the history feature.   By default,
  771. history is available if  the feature readline is false.  To  enable this
  772. feature, remembering the  last 50 commands, put the following  into your
  773. ~/.plrc file:
  774.  
  775. :- set_feature(history, 50).
  776.  
  777. The history  system allows the  user to compose  new queries from  those
  778. typed before and  remembered by the system.   It also allows to  correct
  779. queries and  syntax errors.    SWI-Prolog does  not offer  the Unix  csh
  780. capabilities to  include arguments.   This is omitted  as it is  unclear
  781. how the first, second, etc. argument should be defined.
  782.  
  783. The available  history commands  are shown  in table  2.1.   Figure  2.1
  784. gives some examples.
  785.        __________________________________________________________
  786.        | !!.            |Repeat last query                        |
  787.  
  788.        | !nr.           |Repeat query numbered <_n_r>               |
  789.        | !str.          |Repeat last query starting with <_s_t_r>    |
  790.        | !?str.         |Repeat last query holding <_s_t_r>          |
  791.        | ^old^new.      |Substitute <_o_l_d> into <_n_e_w> of last query |
  792.        | !nr^old^new.   |Substitute in query numbered <_n_r>        |
  793.        | !str^old^new.  |Substitute in query starting with <_s_t_r>  |
  794.        | !?str^old^new. |Substitute in query holding <_s_t_r>        |
  795.        | h.             |Show history list                        |
  796.  
  797.        |_!h.____________|Show_this_list__________________________ |
  798.  
  799.                       Table 2.1:  History commands
  800.  
  801. /staff/jan/.plrc consulted, 0.066667 seconds, 591 bytes
  802. Welcome to SWI-Prolog (Version \plversion)
  803.  
  804. Copyright (c) 1993-1996 University of Amsterdam.  All rights reserved.
  805.  
  806. For help, use ?- help(Topic). or ?- apropos(Word).
  807.  
  808. 1 ?- append("Hello ", "World", L).
  809.  
  810. L = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
  811.  
  812. Yes
  813. 2 ?- !!, writef('L = %s\n', [L]).
  814. append("Hello ", "World", L), writef('L = %s\n', [L]).
  815. L = Hello World
  816.  
  817. L = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
  818.  
  819. Yes
  820. 3 ?- sublist(integer, [3, f, 3.4], L).
  821.  
  822. L = [3]
  823.  
  824. Yes
  825. 4 ?- ^integer^number.
  826.  
  827. sublist(number, [3, f, 3.4], L).
  828.  
  829. L = [3, 3.400000]
  830.  
  831. Yes
  832. 5 ?- h.
  833.     1   append("Hello ", "World", L).
  834.     2   append("Hello ", "World", L), writef('L = %s\n', [L]).
  835.  
  836.     3   sublist(integer, [3, f, 3.4], L).
  837.     4   sublist(number, [3, f, 3.4], L).
  838.  
  839. 5 ?- !2^World^Universe.
  840. append("Hello ", "Universe", L), writef('L = %s\n', [L]).
  841. L = Hello Universe
  842.  
  843. L = [72, 101, 108, 108, 111, 32, 85, 110, 105, 118, 101, 114, 115, 101]
  844.  
  845. Yes
  846. 6 ?- halt.
  847.  
  848.            Figure 2.1:  Some examples of the history facility
  849.  
  850.  
  851. 22..44..11 LLiimmiittaattiioonnss ooff tthhee HHiissttoorryy SSyysstteemm
  852.  
  853. When  in   top  level   SWI-Prolog  reads  the   user's  queries   using
  854. rreeaadd__hhiissttoorryy//66 rather  than rreeaadd//11.    This  predicate first  reads  the
  855. current input  stream up to  a full stop.   While doing  so it maps  all
  856. contiguous blank  space onto  a single  space and  deletes /* ...*/  and
  857. % ...<_c_r> comments.   Parts between double quotes  (") or single  quotes
  858. (') are  left unaltered.   Note that  a Prolog full  stop consists of  a
  859. `non-symbol' character,  followed by a period  (.), followed by a  blank
  860. character.    `Symbol' characters  are:   #$&*+-./:<=>?@^`~.   A  single
  861. quote immediately preceded  by a digit (0-9)  is considered part of  the
  862. <_d_i_g_i_t>'<_d_i_g_i_t>...(e.g. 2'101; binary number 101) sequence.
  863.  
  864. After this initial parsing  the result is first checked for  the special
  865. ^<_o_l_d>^<_n_e_w>.construction.  If this fails the string is  checked for all
  866. occurrences of  the !,  followed by  a !,  ?, a  digit, a  letter or  an
  867. underscore.   These special sequences  are analysed and the  appropriate
  868. substitution from the history list is made.
  869.  
  870. From the  above it  follows that  it is  hard or  impossible to  correct
  871. quotation with single or double quotes, comment delimiters and spacing.
  872.  
  873.  
  874. 22..55 RReeuussee ooff ttoopplleevveell bbiinnddiinnggss
  875.  
  876. Bindings resulting from the successful execution of a  toplevel goal are
  877. asserted in a database.  These values may be  reused in further toplevel
  878. queries as $Var.  Only the latest binding is available.  Example:
  879. 1 ?- maplist(plus(1), "hello", X).
  880.  
  881. X = [105,102,109,109,112]
  882.  
  883. 2 ?- format('~s~n', [$X]).
  884. ifmmp
  885.  
  886.                  Figure 2.2:  Reusing toplevel bindings
  887.  
  888. Note that variables may be set by executing =//22:
  889.  
  890. 6 ?- X = statistics.
  891.  
  892. X = statistics
  893.  
  894. 7 ?- $X.
  895. 28.00 seconds cpu time for 183,128 inferences
  896. 4,016 atoms, 1,904 functors, 2,042 predicates, 52 modules
  897. 55,915 byte codes; 11,239 external references
  898.  
  899.                       Limit    Allocated       In use
  900. Heap         :                                624,820 Bytes
  901. Local  stack :    2,048,000        8,192          404 Bytes
  902. Global stack :    4,096,000       16,384          968 Bytes
  903. Trail  stack :    4,096,000        8,192          432 Bytes
  904.  
  905.  
  906. 22..66 OOvveerrvviieeww ooff tthhee DDeebbuuggggeerr
  907.  
  908. SWI-Prolog has  a 6-port  tracer, extending the  standard 4-port  tracer
  909. [Clocksin & Melish, 1987]  with two  additional  ports.    The  optional
  910. _u_n_i_f_y port allows  the user to inspect  the result after unification  of
  911. the head.  The _e_x_c_e_p_t_i_o_n port shows exceptions raised  by tthhrrooww//11 or one
  912. of the built-in predicates.  See section 3.8.
  913.  
  914. The standard  ports are called call,  exit, redo, fail  and unify.   The
  915. tracer is started  by the ttrraaccee//00 command,  when a spy point is  reached
  916. and the system is  in debugging mode (see ssppyy//11 and ddeebbuugg//00) or  when an
  917. exception is raised.
  918.  
  919. The interactive  toplevel goal ttrraaccee//00 means  ``trace the next  query''.
  920. The tracer shows the  port, displaying the port name, the  current depth
  921. of the  recursion and the goal.   The goal  is printed using the  Prolog
  922. predicate pprriinntt//11  (default), wwrriittee//11  or ddiissppllaayy//11.   An example  using
  923. all ports is shown in figure 2.3.
  924. Yes
  925. 2 ?- visible(+all), leash(-exit).
  926.  
  927. Yes
  928. 3 ?- trace, min([3, 2], X).
  929.   Call:  ( 3) min([3, 2], G235) ? creep
  930.   Unify: ( 3) min([3, 2], G235)
  931.   Call:  ( 4) min([2], G244) ? creep
  932.   Unify: ( 4) min([2], 2)
  933.  
  934.   Exit:  ( 4) min([2], 2)
  935.   Call:  ( 4) min(3, 2, G235) ? creep
  936.   Unify: ( 4) min(3, 2, G235)
  937.   Call:  ( 5) 3 < 2 ? creep
  938.   Fail:  ( 5) 3 < 2 ? creep
  939.   Redo:  ( 4) min(3, 2, G235) ? creep
  940.   Exit:  ( 4) min(3, 2, 2)
  941.   Exit:  ( 3) min([3, 2], 2)
  942.  
  943.                        Figure 2.3:  Example trace
  944.  
  945. On _l_e_a_s_h_e_d  _p_o_r_t_s (set  with the  predicate lleeaasshh//11,  default are  call,
  946. exit, redo and  fail) the user is prompted  for an action.  All  actions
  947. are single character  commands which are executed wwiitthhoouutt waiting  for a
  948. return, unless the command line option -tty is active.  Tracer options:
  949.  
  950. + ((SSppyy))
  951.     Set a spy point (see ssppyy//11) on the current predicate.
  952.  
  953. - ((NNoo ssppyy))
  954.     Remove the spy point (see nnoossppyy//11) from the current predicate.
  955.  
  956. / ((FFiinndd))
  957.     Search  for a port.   After the  `/', the user  can enter a line  to
  958.     specify  the port to  search for.   This line consists  of a set  of
  959.     letters  indicating the  port type,  followed by  an optional  term,
  960.     that  should unify with  the goal run by  the port.   If no term  is
  961.     specified  it is taken as a variable, searching for any port  of the
  962.     specified  type.  If an atom is given, any goal whose  functor has a
  963.     name equal to that atom matches.  Examples:
  964.  
  965.             /f               Search for any fail port
  966.             /fe solve        Search for a  fail or exit  port of
  967.                              any goal with name solve
  968.             /c solve(a, _)   Search for a call to  solve/2 whose
  969.  
  970.                              first argument is a variable or the
  971.                              atom a
  972.             /a member(_, _)  Search for  any  port on  mmeemmbbeerr//22.
  973.                              This is equivalent to setting a spy
  974.                              point on mmeemmbbeerr//22.
  975.  
  976. . ((RReeppeeaatt ffiinndd))
  977.     Repeat the last find command (see `/')
  978.  
  979. A ((AAlltteerrnnaattiivveess))
  980.     Show all goals that have alternatives.
  981.  
  982. C ((CCoonntteexxtt))
  983.     Toggle  `Show Context'.   If on  the context module  of the goal  is
  984.     displayed between square brackets (see section 4).  Default is off.
  985.  
  986. L ((LLiissttiinngg))
  987.     List the current predicate with lliissttiinngg//11.
  988.  
  989. a ((AAbboorrtt))
  990.     Abort Prolog execution (see aabboorrtt//00).
  991.  
  992. b ((BBrreeaakk))
  993.     Enter a Prolog break environment (see bbrreeaakk//00).
  994.  
  995. c ((CCrreeeepp))
  996.     Continue execution, stop at next port.  (Also return, space).
  997.  
  998. d ((DDiissppllaayy))
  999.     Write goals using the Prolog predicate ddiissppllaayy//11.
  1000.  
  1001. e ((EExxiitt))
  1002.     Terminate Prolog (see hhaalltt//00).
  1003.  
  1004. f ((FFaaiill))
  1005.     Force failure of the current goal
  1006.  
  1007. g ((GGooaallss))
  1008.     Show the list of  parent goals (the execution stack).  Note that due
  1009.     to  tail recursion optimization a  number of parent goals might  not
  1010.     exist any more.
  1011.  
  1012. h ((HHeellpp))
  1013.     Show available options (also `?').
  1014.  
  1015. i ((IIggnnoorree))
  1016.     Ignore the current goal, pretending it succeeded.
  1017.  
  1018. l ((LLeeaapp))
  1019.     Continue execution, stop at next spy point.
  1020.  
  1021. n ((NNoo ddeebbuugg))
  1022.     Continue execution in `no debug' mode.
  1023.  
  1024. p ((PPrriinntt))
  1025.     Write goals using the Prolog predicate pprriinntt//11 (default).
  1026.  
  1027. r ((RReettrryy))
  1028.     Undo  all actions (except for database and i/o actions) back  to the
  1029.     call  port of  the current  goal and  resume execution  at the  call
  1030.     port.
  1031.  
  1032. s ((SSkkiipp))
  1033.     Continue  execution,  stop  at the  next  port  of tthhiiss  goal  (thus
  1034.     skipping all calls to children of this goal).
  1035.  
  1036. u ((UUpp))
  1037.     Continue  execution, stop at the next port of tthhee ppaarreenntt  goal (thus
  1038.     skipping  this goal and all calls to  children of this goal).   This
  1039.     option is useful to stop tracing a failure driven loop.
  1040.  
  1041. w ((WWrriittee))
  1042.     Write goals using the Prolog predicate wwrriittee//11.
  1043.  
  1044. The   ideal  4   port  model   as  described   in   many  Prolog   books
  1045. [Clocksin & Melish, 1987] is not visible in many  Prolog implementations
  1046. because  code  optimisation  removes  part  of  the   choice-  and  exit
  1047. points.   Backtrack points  are not shown if  either the goal  succeeded
  1048. deterministically or its alternatives were removed using the cut.   When
  1049. running in  debug mode (ddeebbuugg//00) choice  points are only destroyed  when
  1050. removed by  the cut.    In debug  mode, tail  recursion optimisation  is
  1051. switched off.
  1052.  
  1053.  
  1054. 22..77 CCoommppiillaattiioonn
  1055.  
  1056.  
  1057. 22..77..11 DDuurriinngg pprrooggrraamm ddeevveellooppmmeenntt
  1058.  
  1059. During  program   development,  programs   are  normally  loaded   using
  1060. ccoonnssuulltt//11, or the list  abreviation.  It is common practice  to organise
  1061. a project  as a  collection of source-files  and a  _l_o_a_d_-_f_i_l_e, a  Prolog
  1062. file  containing  only uussee__mmoodduullee//[[11,,22]]  or eennssuurree__llooaaddeedd//11  directives,
  1063. possibly  with a  definition  of the  _e_n_t_r_y_-_p_o_i_n_t  of the  program,  the
  1064. predicate that  is normally used  to start  the program.   This file  is
  1065. often  called load.pl.    If the  entry-point is  called _g_o,  a  typical
  1066. session starts as:
  1067.  
  1068. % pl
  1069. <banner>
  1070.  
  1071. 1 ?- [load].
  1072. <compilation messages>
  1073.  
  1074. Yes
  1075. 2 ?- go.
  1076. <program interaction>
  1077.  
  1078. When  using  Windows,  the  user  may  open  load.pl  from  the  Windows
  1079. explorer,  which will cause  plwin.exe to  be started  in the  directory
  1080. holding load.pl.  Prolog loads load.pl before entering the toplevel.
  1081.  
  1082.  
  1083. 22..77..22 FFoorr rruunnnniinngg tthhee rreessuulltt
  1084.  
  1085. There are  various options if  you want to make  your program ready  for
  1086. real usage.   The best  choice depends on whether  the program is to  be
  1087. used only  on machines  holding the SWI-Prolog  development system,  the
  1088. size of the program and the operating system (Unix vs. Windows).
  1089.  
  1090.  
  1091. 22..77..22..11 CCrreeaattiinngg aa sshheellll--ssccrriipptt
  1092.  
  1093. Especially  on  Unix systems  and  not-too-large  applications,  writing
  1094. a  shell-script  that  simply  loads  your  application  and  calls  the
  1095. entry-point is often a good choice.  A skeleton for  the script is given
  1096. below, followed by the Prolog code to obtain the program arguments.
  1097.  
  1098. #!/bin/sh
  1099.  
  1100. base=<absolute-path-to-source>
  1101. PL=pl
  1102.  
  1103. exec $PL -f none -g "load_files(['$base/load'],[silent(true)])" -t go -
  1104. - $*
  1105.  
  1106. go :-
  1107.         unix(argv(Arguments)),
  1108.         append(_SytemArgs, [--|Args], Arguments), !,
  1109.         go(Args).
  1110.  
  1111. go(Args) :-
  1112.         ...
  1113.  
  1114. On Windows  systems, similar  behaviour can  be achieved  by creating  a
  1115. shortcut to Prolog, passing the proper options or writing a .bat file.
  1116.  
  1117.  
  1118. 22..77..22..22 CCrreeaattiinngg aa ssaavveedd--ssttaattee
  1119.  
  1120. For larger programs,  as well as for  programs that are required run  on
  1121. systems that  do not have the  SWI-Prolog development system  installed,
  1122. creating a saved state is  the best solution.  A saved state  is created
  1123. using qqssaavvee__pprrooggrraamm//[[11,,22]] or using  the linker plld(1).   A saved  state
  1124. is a file  containing machine-independent intermediate code in a  format
  1125. dedicated for fast loading.  Optionally, the  emulator may be integrated
  1126. in  the saved  state,  creating  a single-file,  but  machine-dependent,
  1127. exectable.  This process is descriped in chapter 6.
  1128.  
  1129.  
  1130. 22..77..22..33 CCoommppiilliillaattiioonn uussiinngg tthhee --cc ccoommmmaannddlliinnee ooppttiioonn
  1131.  
  1132. This mechanism is mostly  for backward compatibility.  It  creates files
  1133. in the  same format  as saved-states created  using qqssaavvee__pprrooggrraamm//[[11,,22]],
  1134. but  the resulting  file  is a  translation  of the  source-files  read,
  1135. rather than  a translation of the  state of the  machine.  Unlike  saved
  1136. states, programs  created using -c file do  not include the Prolog  part
  1137. of the development  system.  The result  is very dependent on the  local
  1138. SWI-Prolog installation.
  1139.  
  1140. The command below is used to compile one or more source-files.
  1141.  
  1142. pl [options] [-o output] -c file ...
  1143.  
  1144. The individual source  files may include other files using  the standard
  1145. list notation,  ccoonnssuulltt//11,  eennssuurree__llooaaddeedd//11and  uussee__mmoodduullee//[[11,,22]].    When
  1146. the -o file option is  omitted a file named a.out is created  that holds
  1147. the intermediate code file.
  1148.  
  1149. Intermediate  code files  start with  the  Unix magic  code #!  and  are
  1150. executable.  This implies they can be started as a command:
  1151.  
  1152. % pl -o my_program -c ...
  1153. ...
  1154. % my_program [options]
  1155.  
  1156. Alternatively, my_program can be started with
  1157.  
  1158. % pl -x my_program [options]
  1159.  
  1160. The  following  restrictions  apply to  source  files  that  are  to  be
  1161. compiled with `-c':
  1162.  
  1163.   o tteerrmm__eexxppaannssiioonn//22 should  not  use aasssseerrtt//11  and or  rreettrraacctt//11  other
  1164.     than for local computational purposes.
  1165.  
  1166.   o Files  can  only be  included by  the  standard include  directives:
  1167.     [...],   ccoonnssuulltt//11,  eennssuurree__llooaaddeedd//11 and  uussee__mmoodduullee//[[11,,22]].     User
  1168.     defined loading predicate invocations will not be compiled.
  1169.  
  1170. Directives  are  executed  both when  compiling  the  program  and  when
  1171. loading the intermediate code file.
  1172.  
  1173.  
  1174. 22..88 EEnnvviirroonnmmeenntt CCoonnttrrooll
  1175.  
  1176. The current  system defines 3 different  mechanisms to query and/or  set
  1177. properties  of the  environment:    pplleeaassee//33,  ffllaagg//33 and  ffeeaattuurree//22  as
  1178. well  as a  number of  special purpose  predicates  of which  uunnkknnoowwnn//22,
  1179. ffiilleeeerrrroorrss//22 are examples.  The ISO standard defines prolog_flag.  It is
  1180. likely that all  these global features will  be merged into a single  in
  1181. the future.
  1182.  
  1183.  
  1184. pplleeaassee((_+_K_e_y_, _-_O_l_d_, _+_N_e_w))
  1185.     The  predicate pplleeaassee//33  is  a solution  to avoid  large numbers  of
  1186.     environment  control  predicates.     Later  versions  will  support
  1187.     other  environment  control  as  now  provided  via  the  predicates
  1188.     ssttyyllee__cchheecckk//11,  lleeaasshh//11,  uunnkknnoowwnn//22,  the  tracer  predicates,  etc.
  1189.     These  predicates  are  then  moved into  a  library  for  backwards
  1190.     compatibility.  The currently available options are:
  1191.  
  1192.     ooppttiimmiissee _o_n_/_o_f_f (default:  _o_f_f_)
  1193.          Switch optimise  mode  for the  compiler on  or  off (see  also
  1194.          the command line  option -O).   Currently optimise  compilation
  1195.          only  implies  compilation  of  arithmetic,   making  it  fast,
  1196.          but  invisible to  the  tracer.    Later versions  might  imply
  1197.          various  other optimisations  such  as incorporating  a  number
  1198.          of basic  predicates  in the  virtual machine  (vvaarr//11,  ffaaiill//00,
  1199.          =/2,  etc.)    to  gain speed  at  the  cost of  crippling  the
  1200.          debugger.  Also source level optimisations  such as integrating
  1201.          small  predicates  into  their  callers,  eliminating  constant
  1202.          expressions and  other  predictable constructs.    Source  code
  1203.          optimisation is never  applied to predicates that are  declared
  1204.          dynamic (see ddyynnaammiicc//11).
  1205.  
  1206.     aauuttoollooaadd _o_n_/_o_f_f (default:  _o_n_)
  1207.          If on  autoloading of  library functions is  enabled.   If  off
  1208.          autoloading is disabled.  See section 2.9.
  1209.  
  1210.     vveerrbboossee__aauuttoollooaadd _o_n_/_o_f_f (default:  _o_f_f_)
  1211.          If on the normal  consult message will be printed if  a library
  1212.          is  autoloaded.     By  default  this  message  is  suppressed.
  1213.          Intended to  be used  for debugging purposes  (e.g. where  does
  1214.          this predicate come from?).
  1215.  
  1216.  
  1217. ffeeaattuurree((_?_K_e_y_, _-_V_a_l_u_e))
  1218.     The  predicate   ffeeaattuurree//22  defines  an  interface  to  installation
  1219.     features:   options  compiled in,  version, home,  etc.   With  both
  1220.     arguments unbound, it  will generate all defined features.  With the
  1221.     `Key'  instantiated it  unify the value  of the  feature.   Features
  1222.     come in three  types:  boolean features, features with an atom value
  1223.     and  features with an integer value.  A boolean feature is  true iff
  1224.     the  feature is present aanndd the _V_a_l_u_e  is the atom true.   Currently
  1225.     defined keys:
  1226.  
  1227.     aarrcchh ((_a_t_o_m))
  1228.          Identifier for the hardware and operating system  SWI-Prolog is
  1229.          running on.  Used  to determine the startup file as well  as to
  1230.          select foreign  files for  the right  architecture.   See  also
  1231.          llooaadd__ffoorreeiiggnn//55.
  1232.  
  1233.     vveerrssiioonn ((_i_n_t_e_g_e_r))
  1234.          The version identifier is an integer with value:
  1235.  
  1236.                           10000_*Major+ 100_*Minor+_P_a_t_c_h
  1237.  
  1238.          Note  that  in  releases  upto  2.7.10  this   feature  yielded
  1239.          an  atom   holding  the  three   numbers  separated  by   dots.
  1240.          The  current representation  is  much easier  for  implementing
  1241.          version-conditional statements.
  1242.  
  1243.     hhoommee ((_a_t_o_m))
  1244.          SWI-Prolog's  notion  of   the  home-directory.      SWI-Prolog
  1245.          uses  it's  home  directory   to  find  its  startup  file   as
  1246.          <_h_o_m_e>/startup/startup.<_a_r_c_h> and  to   find  its  library   as
  1247.          <_h_o_m_e>/library.
  1248.  
  1249.     ppiippee ((_b_o_o_l))
  1250.          If true, tell(pipe(command)), etc. are supported.
  1251.  
  1252.     llooaadd__ffoorreeiiggnn ((_b_o_o_l))
  1253.          If true, llooaadd__ffoorreeiiggnn//[[22,,55]]are implemented.
  1254.  
  1255.     ooppeenn__sshhaarreedd__oobbjjeecctt ((_b_o_o_l))
  1256.          If  true,  ooppeenn__sshhaarreedd__oobbjjeecctt//22 and  friends  are  implemented,
  1257.          providing  access  to shared  libraries  (.so  files).     This
  1258.          requires the C-library  functions dlopen() and friends as  well
  1259.          as the configuration option --with-dlopen.
  1260.  
  1261.     ddyynnaammiicc__ssttaacckkss ((_b_o_o_l))
  1262.          If true,  the system uses some  form of `sparse-memory  manage-
  1263.          ment' to realise the stacks.  If  false, malloc()/realloc() are
  1264.          used for  the stacks.   In earlier  days this had  consequenses
  1265.          for foreign code.   As  of version 2.5,  this is no longer  the
  1266.          case.
  1267.  
  1268.          Systems using  `sparse-memory management' are  a bit faster  as
  1269.          there is no  stack-shifter, and checking the stack-boundary  is
  1270.          often realised  by the hardware  using a  `guard-page'.   Also,
  1271.          memory  is actually  returned to  the  system after  a  garbage
  1272.          collection or call  to ttrriimm__ssttaacckkss//00 (called by pprroolloogg//00  after
  1273.          finishing a user-query).
  1274.  
  1275.     cc__lliibbss ((_a_t_o_m))
  1276.          Libraries passed  to the C-linker  when SWI-Prolog was  linked.
  1277.          May  be  used to  determine  the  libraries  needed  to  create
  1278.          statically linked extensions for SWI-Prolog.  See section 5.7.
  1279.  
  1280.     cc__ssttaattiicclliibbss ((_a_t_o_m))
  1281.          On  some machines,  the  SWI-Prolog executable  is  dynamically
  1282.          linked, but  requires some libraries  to be statically  linked.
  1283.          Obsolete.
  1284.  
  1285.     cc__cccc ((_a_t_o_m))
  1286.          Name of the  C-compiler used to  compile SWI-Prolog.   Normally
  1287.          either gcc or cc.  See section 5.7.
  1288.  
  1289.     cc__llddffllaaggss ((_a_t_o_m))
  1290.          Special  linker  flags  passed   to  link  SWI-Prolog.      See
  1291.          section 5.7.
  1292.  
  1293.     ssaavvee ((_b_o_o_l))
  1294.          If true,  ssaavvee//[[11,,22]] is implemented.    Saving using ssaavvee//00  is
  1295.          obsolete.  See qqssaavvee__pprrooggrraamm//[[11,,22]].
  1296.  
  1297.     ssaavvee__pprrooggrraamm ((_b_o_o_l))
  1298.          If  true,  ssaavvee__pprrooggrraamm//[[11,,22]] is  implemented.    Saving  using
  1299.          ssaavvee__pprrooggrraamm//00is obsolete.  See qqssaavvee__pprrooggrraamm//[[11,,22]].
  1300.  
  1301.     rreeaaddlliinnee ((_b_o_o_l))
  1302.          If  true,  SWI-Prolog  is linked  with  the  readline  library.
  1303.          This is  done by  default if  you have  this library  installed
  1304.          on your  system.    It is  also true  for  the Win32  plwin.exe
  1305.          version of SWI-Prolog, which realises a subset  of the readline
  1306.          functionality.
  1307.  
  1308.     ssaavveedd__pprrooggrraamm ((_b_o_o_l))
  1309.          If  true,   Prolog  is   started  from   a  state  saved   with
  1310.          qqssaavvee__pprrooggrraamm//[[11,,22]].
  1311.  
  1312.     rruunnttiimmee ((_b_o_o_l))
  1313.          If true,  SWI-Prolog  is compiled  with  -DO_RUNTIME,  disabling
  1314.          various useful development  features (currently the tracer  and
  1315.          profiler).
  1316.  
  1317.     mmaaxx__iinntteeggeerr ((_i_n_t_e_g_e_r))
  1318.          Maximum  integer  value.     Most  arithmetic  operations  will
  1319.          automatically convert  to floats if  integer values above  this
  1320.          are returned.
  1321.  
  1322.     mmiinn__iinntteeggeerr ((_i_n_t_e_g_e_r))
  1323.          Minimum integer value.
  1324.  
  1325.     mmaaxx__ttaaggggeedd__iinntteeggeerr ((_i_n_t_e_g_e_r))
  1326.          Maximum integer value represented as a `tagged' value.   Tagged
  1327.          integers require  4-bytes storage  and are  used for  indexing.
  1328.          Larger integers are represented as `indirect data'  and require
  1329.          16-bytes  on  the  stacks  (though  a  copy   requires  only  4
  1330.          additional bytes).
  1331.  
  1332.     mmiinn__ttaaggggeedd__iinntteeggeerr ((_i_n_t_e_g_e_r))
  1333.          Start of the tagged-integer value range.
  1334.  
  1335.     ffllooaatt__ffoorrmmaatt ((_a_t_o_m))
  1336.          C printf()  format specification  used by  wwrriittee//11 and  friends
  1337.          to determine  how  floating point  numbers are  printed.    The
  1338.          default is %g.  May be changed.  The  specified value is passed
  1339.          to printf()  without further  checking.   For  example, if  you
  1340.          want more  digits printed,  %.12g will print  all floats  using
  1341.          12 digits  instead of the  default 6.   See also  ffoorrmmaatt//[[11,,22]],
  1342.          wwrriittee//11, pprriinntt//11 and ppoorrttrraayy//11.
  1343.  
  1344.     ccoommppiilleedd__aatt ((_a_t_o_m))
  1345.          Describes when the  system has been  compiled.  Only  available
  1346.          if  the C-compiler  used  to  compile SWI-Prolog  provides  the
  1347.          __DATE__and __TIME__macros.
  1348.  
  1349.     cchhaarraacctteerr__eessccaappeess ((_b_o_o_l))
  1350.          If true  (default),  rreeaadd//11 interprets  \  escape sequences  in
  1351.          quoted atoms and strings.  May be changed.
  1352.  
  1353.     aallllooww__vvaarriiaabbllee__nnaammee__aass__ffuunnccttoorr ((_b_o_o_l))
  1354.          If true  (default  is false),  Functor(arg) is  read  as if  it
  1355.          was written 'Functor'(arg).   Some applications use the  Prolog
  1356.          rreeaadd//11  predicate for  reading  an application  defined  script
  1357.          language.   In these  cases, it is  often difficult to  explain
  1358.          none-Prolog  users  of  the  application  that   constants  and
  1359.          functions can only  start with a  lowercase letter.   Variables
  1360.          can be  turned into atoms  starting with  an uppercase atom  by
  1361.          calling rreeaadd__tteerrmm//22using  the option variable_names  and binding
  1362.          the variables to their name.   Using this feature, F(x)  can be
  1363.          turned into valid syntax for such script languages.   Suggested
  1364.          by Robert van Engelen.  SWI-Prolog specific.
  1365.  
  1366.     hhiissttoorryy ((_i_n_t_e_g_e_r))
  1367.          If  >0,  support  Unix  csh(1) like  history  as  described in
  1368.          section 2.4.  Otherwise, only support  reusing commands through
  1369.          the commandline editor.  The default is to set  this feature to
  1370.          0 if a  commandline editor is  provided (see feature  readline)
  1371.          and 15 otherwise.
  1372.  
  1373.     ggcc ((_b_o_o_l))
  1374.          If true (default), the garbage collector is active.   If false,
  1375.          neither garbage-collection,  nor stack-shifts will take  place,
  1376.          even not on explicit request.  May be changed.
  1377.  
  1378.     ttrraaccee__ggcc ((_b_o_o_l))
  1379.          If  true  (false  is  the  default),  garbage  collections  and
  1380.          stack-shifts  will  be  reported on  the  terminal.     May  be
  1381.          changed.
  1382.  
  1383.     mmaaxx__aarriittyy ((_u_n_b_o_u_n_d_e_d))
  1384.          ISO feature describing  there is no  maximum arity to  compound
  1385.          terms.
  1386.  
  1387.     iinntteeggeerr__rroouunnddiinngg__ffuunnccttiioonn ((_d_o_w_n_,_t_o_w_a_r_d___z_e_r_o))
  1388.          ISO  feature  describing rounding  by  //  and  rem  arithmetic
  1389.          functions.  Value depends on the C-compiler used.
  1390.  
  1391.     bboouunnddeedd ((_t_r_u_e))
  1392.          ISO  feature  describing integer  representation  is  bound  by
  1393.          min_integer and min_integer.
  1394.  
  1395.     ttttyy__ccoonnttrrooll ((_b_o_o_l))
  1396.          Determines whether  the terminal  is switched to  raw mode  for
  1397.          ggeett__ssiinnggllee__cchhaarr//11, which  also reads  the  user-actions for  the
  1398.          trace.  May be set.  See also the +/-tty command-line option.
  1399.  
  1400.     ddeebbuugg__oonn__eerrrroorr ((_b_o_o_l))
  1401.          If  true,  start  the  tracer  after   an  error  is  detected.
  1402.          Otherwise just continue  execution.   The goal that raised  the
  1403.          error  will normally  fail.    See  also ffiilleeeerrrroorrss//22  and  the
  1404.          feature report_error.  May be changed.  Default is true, except
  1405.          for the runtime version.
  1406.  
  1407.     rreeppoorrtt__eerrrroorr ((_b_o_o_l))
  1408.          If true, print  error messages, otherwise  suppress them.   May
  1409.          be changed.   See also the debug_on_error feature.  Default  is
  1410.          true, except for the runtime version.
  1411.  
  1412.     uunniixx ((_b_o_o_l))
  1413.          If  true,  the  operating  system  is  some  version  of  Unix.
  1414.          Defined  if the  C-compiler used  to  compile this  version  of
  1415.          SWI-Prolog either defines __unix__ or unix.
  1416.  
  1417.     wwiinnddoowwss ((_b_o_o_l))
  1418.          If  true,   the  operating  system  is  an  implementation   of
  1419.          Microsoft Windows (3.1, 95, NT, etc.).
  1420.  
  1421.  
  1422. sseett__ffeeaattuurree((_+_K_e_y_, _+_V_a_l_u_e))
  1423.     Define a new feature or  change its value.  _K_e_y is an atom, _V_a_l_u_e is
  1424.     an atom or number.
  1425.  
  1426.  
  1427. 22..99 AAuuttoommaattiicc llooaaddiinngg ooff lliibbrraarriieess
  1428.  
  1429. If ---at  runtime--- an undefined predicate  is trapped the system  will
  1430. first try  to import  the predicate  from the  module's default  module.
  1431. If this  fails the _a_u_t_o  _l_o_a_d_e_r is  activated.   On first activation  an
  1432. index to all library files in all library directories  is loaded in core
  1433. (see lliibbrraarryy__ddiirreeccttoorryy//11).   If the undefined  predicate can be  located
  1434. in the one  of the libraries that  library file is automatically  loaded
  1435. and the  call to the  (previously undefined) predicate is  resumed.   By
  1436. default this  mechanism loads the  file silently.   The pplleeaassee//33  option
  1437. verbose_autoload is provided to get verbose loading.   The please option
  1438. autoload can be used to enable/disable the entire auto load system.
  1439.  
  1440. Autoloading  only handles  (library) source  files that  use the  module
  1441. mechanism  described  in  chapter  4.     The  files   are  loaded  with
  1442. uussee__mmoodduullee//22 and only the trapped  undefined predicate will be  imported
  1443. to the module  where the undefined predicate  was called.  Each  library
  1444. directory  must hold  a file  INDEX.pl  that contains  an index  to  all
  1445. library files  in the directory.    This file consists  of lines of  the
  1446. following format:
  1447.  
  1448. index(Name, Arity, Module, File).
  1449.  
  1450. The  predicate mmaakkee//00  scans  the  autoload libraries  and  updates  the
  1451. index if  it exists,  is writable  and out-of-date.   It  is advised  to
  1452. create an empty  file called INDEX.pl in  a library directory meant  for
  1453. auto loading  before doing  anything else.    This index  file can  then
  1454. be updated  by running the prolog  mmaakkee__lliibbrraarryy__iinnddeexx//11(`%' is the  Unix
  1455. prompt):
  1456.  
  1457. % mkdir ~/lib/prolog
  1458. % cd !$
  1459. % pl -g true -t 'make_library_index(.)'
  1460.  
  1461. If  there  are  more than  one  library  files  containing  the  desired
  1462. predicate the following search schema is followed:
  1463.  
  1464.  1. If  a there is a library file  that defines the module in  which the
  1465.     undefined predicate is trapped, this file is used.
  1466.  
  1467.  2. Otherwise  library files  are considered  in the  order they  appear
  1468.     in  the  lliibbrraarryy__ddiirreeccttoorryy//11  predicate  and  within  the  directory
  1469.     alphabetically.
  1470.  
  1471.  
  1472. mmaakkee__lliibbrraarryy__iinnddeexx((_+_D_i_r_e_c_t_o_r_y))
  1473.     Create  an index for this  directory.  The  index is written to  the
  1474.     file  'INDEX.pl' in the specified directory.   Fails with a  warning
  1475.     if the directory does not exist or is write protected.
  1476.  
  1477.  
  1478. 22..99..11 NNootteess oonn AAuuttoommaattiicc LLooaaddiinngg
  1479.  
  1480. The autoloader is a new  feature to SWI-Prolog.  Its aim is  to simplify
  1481. program development and program  management.  Common lisp has  a similar
  1482. feature, but here the user has to specify which library  is to be loaded
  1483. if a specific  function is called which is  not defined.  The  advantage
  1484. of the  SWI-Prolog schema  is that  the user  does not  have to  specify
  1485. this.   The  disadvantage however is  that the  user might be  wondering
  1486. ``where  the hell  this predicate  comes from''.    Only experience  can
  1487. learn  whether  the  functionality of  the  autoloader  is  appropriate.
  1488. Comments are welcome.
  1489.  
  1490. The autoloader only works if the unknown flag (see  uunnkknnoowwnn//22) is set to
  1491. trace (default).  A more appropriate interaction with  this flag will be
  1492. considered.
  1493.  
  1494.  
  1495. 22..1100 GGaarrbbaaggee CCoolllleeccttiioonn
  1496.  
  1497. SWI-Prolog  version  1.4  was  the  first  release  to  support  garbage
  1498. collection.   Together with tail-recursion optimisation this  guaranties
  1499. forward chaining  programs do  not waste indefinite  amounts of  memory.
  1500. Previous releases of this manual stressed on  using failure-driven loops
  1501. in those  cases that  no information  needed to  be passed  to the  next
  1502. iteration via arguments.  This to avoid large amounts of  garbage.  This
  1503. is no longer strictly  necessary, but it should be noticed  that garbage
  1504. collection is a time  consuming activity.  Failure driven loops  tend to
  1505. be faster for this reason.
  1506.  
  1507.  
  1508. 22..1111 SSyynnttaaxx NNootteess
  1509.  
  1510. SWI-Prolog uses  standard `Edinburgh'  syntax.   A  description of  this
  1511. syntax can be found in the Prolog books referenced  in the introduction.
  1512. Below are some  non-standard or non-common constructs that are  accepted
  1513. by SWI-Prolog:
  1514.  
  1515.   o 0'<_c_h_a_r>
  1516.     This  construct is not accepted by all Prolog systems that  claim to
  1517.     have  Edinburgh compatible syntax.  It describes the ASCII  value of
  1518.     <_c_h_a_r>.   To  test whether C is  a lower case character  one can use
  1519.     between(0'a, 0'z, C).
  1520.  
  1521.   o /* .../* ...*/ ...*/
  1522.     The  /* ...*/  comment statement  can be  nested.    This is  useful
  1523.     if  some  code with  /* ...*/  comment statements  in it  should  be
  1524.     commented out.
  1525.  
  1526.  
  1527. 22..1111..11 IISSOO SSyynnttaaxx SSuuppppoorrtt
  1528.  
  1529. SWI-Prolog offers ISO compatible extensions to the Edinburgh syntax.
  1530.  
  1531.  
  1532. 22..1111..11..11 CChhaarraacctteerr EEssccaappee SSyynnttaaxx
  1533.  
  1534. Within quoted  atoms (using single quotes:   '<_a_t_o_m>'special  characters
  1535. are represented  using escape-sequences.    An escape  sequence is  lead
  1536. in  by the  backslash  (\) character.    The  list of  escape  sequences
  1537. is compatible  with the  ISO standard,  but contains  one extension  and
  1538. the interpretation of numerically specified characters  is slightly more
  1539. flexible to improve compatibility.
  1540.  
  1541. \a
  1542.     Alert character.  Normally the ASCII character 7 (beep).
  1543.  
  1544. \b
  1545.     Backspace character.
  1546.  
  1547. \c
  1548.     No  output.    All  input  characters  upto but  not  including  the
  1549.     first  non-layout  character  are skipped.     This allows  for  the
  1550.     specification of pretty-looking  long lines.  For compatibility with
  1551.     Quintus Prolog.  Nor supported by ISO. Example:
  1552.  
  1553.     format('This is a long line that would look better if it was \c
  1554.            split across multiple physical lines in the input')
  1555.  
  1556. \<RETURN>
  1557.     No  output.   Skips input till the  next non-layout character or  to
  1558.     the end of the next line.  Same intention as \c but ISO compatible.
  1559.  
  1560. \f
  1561.     Form-feed character.
  1562.  
  1563. \n
  1564.     Next-line character.
  1565.  
  1566. \r
  1567.     Carriage-return only (i.e. go back to the start of the line).
  1568.  
  1569. \t
  1570.     Horizontal tab-character.
  1571.  
  1572. \v
  1573.     Vertical tab-character (ASCII 11).
  1574.  
  1575. \x23
  1576.     Hexadecimal  specification of a character.   23 is just an  example.
  1577.     The  `x'  may be  followed by  a maximum  of  2 hexadecimal  digits.
  1578.     The  closing \  is optional.   The  code \xa\3  emits the  character
  1579.     10  (hexadecimal `a')  followed by  `3'.   The code  \x201 emits  32
  1580.     (hexadecimal  `20') followed by `1'.  According to ISO,  the closing
  1581.     \  is  obligatory and  the  number  of digits  is  unlimited.    The
  1582.     SWI-Prolog  definition allows for ISO compatible specification,  but
  1583.     is compatible with other implementations.
  1584.  
  1585. \40
  1586.     Octal   character  specification.     The  rules  and   remarks  for
  1587.     hexadecimal  specifications apply to  octal specifications too,  but
  1588.     the maximum allowed number of octal digits is 3.
  1589.  
  1590. \<_c_h_a_r_a_c_t_e_r>
  1591.     Any  character  immediately  preceded by  a  \ is  copied  verbatim.
  1592.     Thus,  '\\' is an atom  consisting of a single  \ and '\'' and  ''''
  1593.     both describe the atom with a single '.
  1594.  
  1595. Character      escaping      is     only      available      if      the
  1596. feature(character_escapes, true) is  active (default).   See  ffeeaattuurree//22.
  1597. Character  escapes  conflict  with  wwrriitteeff//22  in  two  ways:     \40  is
  1598. interpreted as  decimal 40 by wwrriitteeff//22,  but character escapes  handling
  1599. by  read has  already  interpreted  as 32  (40  octal).    Also,  \l  is
  1600. translated to  a single  `l'.   Double  the \  (e.g. \\,  use the  above
  1601. escape sequences or use ffoorrmmaatt//22.
  1602.  
  1603.  
  1604. 22..1111..11..22 SSyynnttaaxx ffoorr NNoonn--DDeecciimmaall NNuummbbeerrss
  1605.  
  1606. SWI-Prolog  implements  both  Edinburgh  and  ISO   representations  for
  1607. non-decimal numbers.   According to  Edinburgh syntax, such numbers  are
  1608. written as <_r_a_d_i_x>'<_n_u_m_b_e_r>, where <_r_a_d_i_x> is  a number between 2 and 36.
  1609. ISO defines binary,  octal and hexadecimal numbers using 0[bxo]<_n_u_m_b_e_r>.
  1610. For example:  A is 0b100 \/ 0xf00  is a valid expression.   Such numbers
  1611. are always unsigned.
  1612.  
  1613.  
  1614. 22..1122 SSyysstteemm LLiimmiittss
  1615.  
  1616.  
  1617. 22..1122..11 LLiimmiittss oonn MMeemmoorryy AArreeaass
  1618.  
  1619. SWI-Prolog has  a number of  memory areas which are  only enlarged to  a
  1620. certain limit.   The  default sizes for these  areas should suffice  for
  1621. most applications, but big  applications may require larger ones.   They
  1622. are modified  by command  line options.    The table  below shows  these
  1623. areas.   The first column  gives the option name  to modify the size  of
  1624. the area.  The option character is immediately followed  by a number and
  1625. optionally by  a k or  m.   With k or  no unit  indicator, the value  is
  1626. interpreted in Kbytes (1024 bytes), with m, the  value is interpreted in
  1627. Mbytes (10241* 024 bytes).
  1628.  
  1629. The  local-, global-  and trail-stack  are limited  to 64  Mbytes on  32
  1630. bit processors,  or more in general to  2 to the power bits-per-long - 6
  1631. bytes.
  1632.        ___________________________________________________________
  1633.        |_Option_|Default_|Area_name______|Description____________|_||-L||2Mllooccaa||llTssttaacckkhe|l||||ocal|stack is used
  1634.  
  1635.        |        |                        to  store   the  execu- |             |            ||
  1636.        |        |                        tion  environments   of |             |            ||
  1637.        |        |                        procedure  invocations. |             |            ||
  1638.        |        |                        The  space for  an  en- |             |            ||
  1639.        |        |                        vironment is  reclaimed |             |            ||
  1640.        |        |                        when  it  fails,  exits |             |            ||
  1641.        |        |                        without leaving  choice |             |            ||
  1642.        |        |                        points,   the  alterna- |             |            ||
  1643.        |        |                        tives are  cut of  with |             |            ||
  1644.        |        |                                                |             |            ||
  1645.  
  1646.        |        |                        the  !/0  predicate  or |             |            ||
  1647.        |        |                        no  choice points  have |             |            ||
  1648.        |        |                        been created  since the |             |            ||
  1649.        |        |                        invocation and the last |             |            ||
  1650.        |        |                        subclause  is   started |             |            ||
  1651.        |        |                        (tail recursion optimi- |             |            ||
  1652.        ||       ||      |               ||sation).                ||            |            ||
  1653.  
  1654.        |   -G   | 4M    |gglloobbaall ssttaacckk   ||Theusglobaled stacktois store| terms
  1655.        |        |       |               ||created during Prolog's |
  1656.        |        |       |               ||execution.    Terms  on |
  1657.        |        |       |               ||                        |
  1658.        |        |       |               ||this stack will  be re- |
  1659.        |        |       |               ||claimed by backtracking |
  1660.        |        |       |               ||to a  point before  the |
  1661.        |        |       |               ||term  was   created  or |
  1662.        |        |       |               ||by  garbage  collection |
  1663.  
  1664.        |        |       |               ||(provided  the term  is |
  1665.        ||       ||      ||              ||no||longer referenced).  ||
  1666.        |   -T   | 4M    |ttrraaiill ssttaacckk    ||Theusetraild stackto isstore|  as-
  1667.        |        |       |               ||signments during execu- |
  1668.        |        |       |               ||                        |
  1669.        |        |       |               ||tion.   Entries on this |
  1670.        |        |       |               ||stack remain  alive un- |
  1671.  
  1672.        |        |       |               ||til backtracking before |
  1673.        |        |       |               ||the  point of  creation |
  1674.        |        |       |               ||or the  garbage collec- |
  1675.        |        |       |               ||tor determines they are |
  1676.        ||       ||      ||              ||nor||needed any longer.  ||
  1677.  
  1678.        |   -A   | 1M    |aarrgguummeenntt ssttaacckk ||Theusargumentestackd isto|  store   one
  1679.        |        |       |               ||of   the   intermediate |
  1680.        |        |       |               ||code interpreter's reg- |
  1681.        |        |       |               ||isters.     The  amount |
  1682.        |        |       |               ||of space needed on this |
  1683.        |        |       |               ||stack is determined en- |
  1684.        |        |       |               ||                        |
  1685.        |        |       |               ||tirely by the  depth in |
  1686.        |        |       |               ||which terms  are nested |
  1687.        |        |       |               ||in  the   clauses  that |
  1688.  
  1689.        |        |       |               ||constitute the program. |
  1690.        |        |       |               ||Overflow is most likely |
  1691.        |        |       |               ||when using long strings |
  1692.        |________|_______|_______________||in_a_clause.____________|_
  1693.  
  1694.                         Table 2.2:  Memory areas
  1695.  
  1696.  
  1697. 22..1122..11..11 TThhee hheeaapp
  1698.  
  1699. With  the heap,  we  refer  to the  memory  area  used by  mmaalllloocc(())  and
  1700. friends.  SWI-Prolog uses the area to store  atoms, functors, predicates
  1701. and their  clauses, records and  other dynamic data.   As of  SWI-Prolog
  1702. 2.8.5, no limits are  imposed on the addresses returned by  mmaalllloocc(()) and
  1703. friends.
  1704.  
  1705. On  some machines,  the  runtime stacks  described above  are  allocated
  1706. using `sparse allocation'.   Virtual space upto the limit is  claimed at
  1707. startup and  committed and released  while the  area grows and  shrinks.
  1708. On Win32  platform this  is realised using  VViirrttuuaallAAlllloocc(()) and  friends.
  1709. On Unix systems this is realised using mmmmaapp(()),  either mapping /dev/zero
  1710. or a temporary file created in /tmp.
  1711.  
  1712. As Unix  provides no  way to  reserve part  of the  address space,  this
  1713. process may  lead to  conflicts.   By default,  SWI-Prolog computes  the
  1714. required virtual address  space for the runtime  stacks.  If  available,
  1715. it uses ggeettrrlliimmiitt(()) to  determine the top of the virtual  space reserved
  1716. for mmaalllloocc(()) usage and locates  the stacks in the top of this area.   It
  1717. assumes no  other mmmmaapp(()) activity  such as  mapping shared libraries  or
  1718. mmmmaapp(()) by foreign  modules will use the  area reserved for the heap  and
  1719. mmaalllloocc(()), mmaalllloocc(())  will grow the  heap from low  to high addresses  and
  1720. will notice the existence of the Prolog stacks.
  1721.  
  1722. These  assumptions appear  to  hold.    The user  may  using the  -H  to
  1723. specify the  maximum heap-size.   In this case,  the Prolog stacks  will
  1724. be allocated  at the indicated  size from the current  top of the  heap.
  1725. On  these system,  ssttaattiissttiiccss//[[00,,22]] reports  heaplimit and  heap.    The
  1726. heaplimit value is the  distance between the _b_r_e_a_k and the  first Prolog
  1727. stack.   The heap value is the  distance between what Prolog assumes  to
  1728. be the base of the heap and the current location of the break.
  1729.  
  1730.  
  1731. 22..1122..22 OOtthheerr LLiimmiittss
  1732.  
  1733. CCllaauusseess  Currently the  following limitations  apply to  clauses.    The
  1734.     arity  may not be more than 1024 and the number of  variables should
  1735.     be less than 65536.
  1736.  
  1737. AAttoommss aanndd SSttrriinnggss  SWI-Prolog  has no  limits  on  the  sizes  of  atoms
  1738.     and  strings.   rreeaadd//11  and its derivatives  however normally  limit
  1739.     the  number  of newlines  in  an  atom or  string  to 5  to  improve
  1740.     error  detection  and recovery.    This  can  be switched  off  with
  1741.     ssttyyllee__cchheecckk//11.
  1742.  
  1743. AAddddrreessss ssppaaccee  SWI-Prolog  data  is  packed  in  a 32-bit  word,   which
  1744.     contains  both type and value information.  The size of  the various
  1745.     memory areas is limited  to 128 Mb for each of the areas.  With some
  1746.     redesign,  the program area could be split into data that  should be
  1747.     within  this range and  the rest of  the data, virtually  unlimiting
  1748.     the program size.
  1749.  
  1750. IInntteeggeerrss  Integers are 32-bit to the  user, but integers upto the  value
  1751.     of the max_tagged_integer feature are represented more efficiently.
  1752.  
  1753. FFllooaattss  Floating  point  numbers   are  represented  as  native   double
  1754.     precision floats, 64 bit IEEE on most machines.
  1755.  
  1756.  
  1757. 22..1122..33 RReesseerrvveedd NNaammeess
  1758.  
  1759. The boot  compiler (see -b  option) does not  support the module  system
  1760. (yet).    As large  parts of  the system  are written  in Prolog  itself
  1761. we  need some  way to  avoid name  clashes with  the user's  predicates,
  1762. database  keys,  etc.    Like  Edinburgh  C-Prolog  [Pereira, 1986]  all
  1763. predicates,  database keys,  etc. that should  be hidden  from the  user
  1764. start with a dollar ($) sign (see ssttyyllee__cchheecckk//11).
  1765.  
  1766. The  compiler uses  the  special  functor $VAR$/1  while  analysing  the
  1767. clause  to  compile.      Using  this   functor  in  a  program   causes
  1768. unpredictable behaviour of the compiler and resulting program.
  1769.  
  1770.  
  1771. CChhaapptteerr 33..  BBUUIILLTT--IINN PPRREEDDIICCAATTEESS
  1772.  
  1773.  
  1774. 33..11 NNoottaattiioonn ooff PPrreeddiiccaattee DDeessccrriippttiioonnss
  1775.  
  1776. We have  tried to  keep the  predicate descriptions  clear and  concise.
  1777. First  the predicate  name is  printed  in bold  face, followed  by  the
  1778. arguments in  italics.   Arguments  are preceded by  a `+',  `-' or  `?'
  1779. sign.    `+'  indicates the  argument is  input  to the  predicate,  `-'
  1780. denotes output and  `?'  denotes `either  input or output'.   Constructs
  1781. like `oopp//33' refer to the predicate `op' with arity `3'.
  1782.  
  1783.  
  1784. 33..22 CCoonnssuullttiinngg PPrroolloogg SSoouurrccee ffiilleess
  1785.  
  1786. SWI-Prolog source files  normally have a suffix  `.pl'.  Specifying  the
  1787. suffix  is optional.    All predicates  that handle  source files  first
  1788. check  whether a  file with  suffix  `.pl' exists.    If  not the  plain
  1789. file name  is checked  for existence.   Library  files are specified  by
  1790. embedding the file name using the functor lliibbrraarryy//11.   Thus `foo' refers
  1791. to `foo.pl'  or `foo'  in the current  directory, `library(foo)'  refers
  1792. to `foo.pl'  or `foo'  in one  of the library  directories specified  by
  1793. the dynamic predicate  lliibbrraarryy__ddiirreeccttoorryy//11.   The user may specify  other
  1794. `aliases'  than library  using the  predicate ffiillee__sseeaarrcchh__ppaatthh//22.    This
  1795. is strongly  encouraged for  managing complex  applications.   See  also
  1796. aabbssoolluuttee__ffiillee__nnaammee//[[22,,33]].
  1797.  
  1798. SWI-Prolog     recognises     grammar    rules     as     defined     in
  1799. [Clocksin & Melish, 1987].     The  user  may   define  additional  com-
  1800. pilation  of  the   source  file  by  defining  the  dynamic   predicate
  1801. tteerrmm__eexxppaannssiioonn//22.     Transformations  by  this  predicate  overrule  the
  1802. systems  grammar  rule transformations.     It  is not  allowed  to  use
  1803. aasssseerrtt//11, rreettrraacctt//11 or  any other database predicate in tteerrmm__eexxppaannssiioonn//22
  1804. other than for local computational purposes.
  1805.  
  1806. Directives  may be  placed  anywhere  in a  source  file,  invoking  any
  1807. predicate.    They are  executed when  encountered.    If the  directive
  1808. fails, a warning is printed.  Directives are specified  by :-/1 or ?-/1.
  1809. There is no difference between the two.
  1810.  
  1811. SWI-Prolog   does   not   have   a   separate   rreeccoonnssuulltt//11   predicate.
  1812. Reconsulting  is  implied automatically  by  the  fact that  a  file  is
  1813. consulted which is already loaded.
  1814.  
  1815.  
  1816. llooaadd__ffiilleess((_+_F_i_l_e_s_, _+_O_p_t_i_o_n_s))
  1817.     The  predicate llooaadd__ffiilleess//22 is the parent  of all the other  loading
  1818.     predicates.    It  currently supports  a subset  of  the options  of
  1819.     Quintus  llooaadd__ffiilleess//22.    _F_i_l_e_s is either  specifies a  single, or  a
  1820.     list  of  source-files.    The specification  for a  source-file  is
  1821.     handled  aabbssoolluuttee__ffiillee__nnaammee//22.   See this predicate for  the supported
  1822.     expansions.  _O_p_t_i_o_n_s is a list of options using the format
  1823.  
  1824.          _O_p_t_i_o_n_N_a_m_e(_O_p_t_i_o_n_V_a_l_u_e)
  1825.  
  1826.     The following options are currently supported:
  1827.  
  1828.     iiff((_C_o_n_d_i_t_i_o_n))
  1829.          Load the  file only  if the specified  condition is  satisfied.
  1830.          The value  true loads the  file unconditionally, changed  loads
  1831.          the file  if it  was not loaded  before, or  has been  modified
  1832.          since it was loaded the last time, not_loaded loads the file if
  1833.          it was not loaded before.
  1834.  
  1835.     mmuusstt__bbee__mmoodduullee((_B_o_o_l))
  1836.          If true,  raise an  error if  the file  is not  a module  file.
  1837.          Used by uussee__mmoodduullee//[[11,,22]].
  1838.  
  1839.     iimmppoorrttss((_L_i_s_t_O_r_A_l_l))
  1840.          If  all and  the  file is  a  module file,  import  all  public
  1841.          predicates.  Otherwise import only the named predicates.   Each
  1842.          predicate is refered to  as <_n_a_m_e>/<_a_r_i_t_y>.  This  option has no
  1843.          effect if the file is not a module file.
  1844.  
  1845.     ssiilleenntt((_B_o_o_l))
  1846.          If  true, load  the  file  without printing  a  message.    The
  1847.          specified  value is  the  default for  all  files loaded  as  a
  1848.          result of loading the specified files.
  1849.  
  1850.  
  1851. ccoonnssuulltt((_+_F_i_l_e))
  1852.     Read  _F_i_l_e as a Prolog  source file.  _F_i_l_e  may be a list of  files,
  1853.     in  which case all members  are consulted in turn.   _F_i_l_e may  start
  1854.     with  the csh(1) special sequences ~,  ~<_u_s_e_r>and $<_v_a_r>.  _F_i_l_e  may
  1855.     also be library(Name),  in which case the libraries are searched for
  1856.     a  file with the specified  name.   See also lliibbrraarryy__ddiirreeccttoorryy//11 and
  1857.     ffiillee__sseeaarrcchh__ppaatthh//22.   ccoonnssuulltt//11 may be  abbreviated by just typing  a
  1858.     number of file names in a list.  Examples:
  1859.  
  1860.         ?- consult(load).       % consult load or load.pl
  1861.         ?- [library(quintus)].  % load Quintus compatibility library
  1862.  
  1863.     Equivalent to load_files(Files, []).
  1864.  
  1865.  
  1866. eennssuurree__llooaaddeedd((_+_F_i_l_e))
  1867.     If the file  is not already loaded, this is equivalent to ccoonnssuulltt//11.
  1868.     Otherwise,   if  the  file  defines  a  module,  import  all  public
  1869.     predicates.    Finally,  if the  file is  already loaded,  is not  a
  1870.     module  file and the context module  is not the global user  module,
  1871.     eennssuurree__llooaaddeedd//11 will call ccoonnssuulltt//11.
  1872.  
  1873.     With the semantics, we  hope to get as closely possible to the clear
  1874.     semantics  without the presence  of a module  system.   Applications
  1875.     using modules should consider using uussee__mmoodduullee//[[11,,22]].
  1876.  
  1877.     Equivalent to load_files(Files, [if(changed)]).
  1878.  
  1879.  
  1880. rreeqquuiirree((_+_L_i_s_t_O_f_N_a_m_e_A_n_d_A_r_i_t_y))
  1881.     Declare  that  this file/module  requires the  specified  predicates
  1882.     to  be defined ``with  their commonly accepted  definition''.   This
  1883.     predicate originates from  the Prolog portability layer for XPCE. It
  1884.     is  intended to  provide a  portable mechanism  for specifying  that
  1885.     this module requires the specified predicates.
  1886.  
  1887.     The implementation normally  first verifies whether the predicate is
  1888.     already defined.   If not, it will search the libraries and load the
  1889.     required library.
  1890.  
  1891.     SWI-Prolog, having autoloading,  does nnoott load the library.  Instead
  1892.     it  creates a procedure  header for the  predicate if this does  not
  1893.     exist.    This will flag  the predicate  as `undefined'.   See  also
  1894.     cchheecckk//00 and aauuttoollooaadd//00.
  1895.  
  1896.  
  1897. mmaakkee
  1898.     Consult  all source  files that  have been changed  since they  were
  1899.     consulted.   It checks _a_l_l  loaded source files:  files loaded  into
  1900.     a  compiled state  using pl -c ...  and files  loaded using  consult
  1901.     or  one of  its  derivatives.   mmaakkee//00  is normally  invoked by  the
  1902.     eeddiitt//[[00,,11]]  and eedd//[[00,,11]] predicates.   mmaakkee//00  can be combined  with
  1903.     the  compiler to speed  up the  development of large  packages.   In
  1904.     this case compile the package using
  1905.  
  1906.     sun% pl -g make -o my_program -c file ...
  1907.  
  1908.     If  `my_program' is started it will first reconsult  all source files
  1909.     that have changed since the compilation.
  1910.  
  1911.  
  1912. lliibbrraarryy__ddiirreeccttoorryy((_?_A_t_o_m))
  1913.     Dynamic  predicate used  to specify  library directories.    Default
  1914.     ./lib,  ~/lib/prolog and  the system's library  (in this order)  are
  1915.     defined.    The  user may  add library  directories using  aasssseerrtt//11,
  1916.     aasssseerrttaa//11 or remove system defaults using rreettrraacctt//11.
  1917.  
  1918.  
  1919. ffiillee__sseeaarrcchh__ppaatthh((_+_A_l_i_a_s_, _?_P_a_t_h))
  1920.     Dynamic  predicate used to specify `path-aliases'.  This  feature is
  1921.     best described using an example.  Given the definition
  1922.  
  1923.     file_search_path(demo, '~/demo').
  1924.  
  1925.     the   file   specification   demo(myfile)  will   be   expanded   to
  1926.     ~/demo/myfile.    The second  argument of  ffiillee__sseeaarrcchh__ppaatthh//22may  be
  1927.     another alias.
  1928.  
  1929.     Below  is the  initial definition  of the file  search path.    This
  1930.     path  implies  swi(<_P_a_t_h>)  refers  to  a  file  in  the  SWI-Prolog
  1931.     home   directory.     The  alias  foreign(<_P_a_t_h>) is   intended  for
  1932.     storing   shared  libraries  (.so  or   .DLL  files).     See   also
  1933.     llooaadd__ffoorreeiiggnn__lliibbrraarryy//[[11,,22]].
  1934.  
  1935.     user:file_search_path(library, X) :-
  1936.             library_directory(X).
  1937.     user:file_search_path(swi, Home) :-
  1938.             feature(home, Home).
  1939.     user:file_search_path(foreign, swi(ArchLib)) :-
  1940.             feature(arch, Arch),
  1941.             concat('lib/', Arch, ArchLib).
  1942.     user:file_search_path(foreign, swi(lib)).
  1943.  
  1944.     The  ffiillee__sseeaarrcchh__ppaatthh//22expansion is used  by all loading  predicates
  1945.     as well as by aabbssoolluuttee__ffiillee__nnaammee//22.
  1946.  
  1947.  
  1948. eexxppaanndd__ffiillee__sseeaarrcchh__ppaatthh((_+_S_p_e_c_, _-_P_a_t_h))
  1949.     Unifies  _P_a_t_h  will   all  possible  expansions  of  the  file  name
  1950.     specification _S_p_e_c.  See also aabbssoolluuttee__ffiillee__nnaammee//33.
  1951.  
  1952.  
  1953. ssoouurrccee__ffiillee((_?_F_i_l_e))
  1954.     Succeeds  if  _F_i_l_e was  loaded using  ccoonnssuulltt//11  or eennssuurree__llooaaddeedd//11.
  1955.     _F_i_l_e   refers   to   the  full   path   name   of  the   file   (see
  1956.     eexxppaanndd__ffiillee__nnaammee//22).  The predicate ssoouurrccee__ffiillee//11 backtracks over all
  1957.     loaded source files.
  1958.  
  1959.  
  1960. ssoouurrccee__ffiillee((_?_P_r_e_d_, _?_F_i_l_e))
  1961.     Is  true if  the predicate specified  by _P_r_e_d  was loaded from  file
  1962.     _F_i_l_e,  where _F_i_l_e is an absolute path  name (see eexxppaanndd__ffiillee__nnaammee//22).
  1963.     Can  be used with any  instantiation pattern, but the database  only
  1964.     maintains  the source file for each predicate.   Predicates declared
  1965.     multifile (see mmuullttiiffiillee//11) cannot be found this way.
  1966.  
  1967.  
  1968. pprroolloogg__llooaadd__ccoonntteexxtt((_?_K_e_y_, _?_V_a_l_u_e))
  1969.     Determine loading context.  The following keys are defined:
  1970.  
  1971.       ________________________________________________________________
  1972.       |__KKeeyy______________________||DDeessccrriippttiioonn________________________________________________________________________||
  1973.       || module        |Module into which file is loaded               |
  1974.       | file          |File loaded                                    |
  1975.       | stream        |Stream identifier (see ccuurrrreenntt__iinnppuutt//11)        |
  1976.  
  1977.       | directory     |Directory in which _F_i_l_e lives.                 |
  1978.       | term_position |Position of last  term read.  Term of  the form|
  1979.       |_______________|'$stream_position'(0,<_L_i_n_e>,0,0,0)_____________|
  1980.  
  1981.     Quintus compatibility predicate.  See also ssoouurrccee__llooccaattiioonn//22.
  1982.  
  1983.  
  1984. ssoouurrccee__llooccaattiioonn((_-_F_i_l_e_, _-_L_i_n_e))
  1985.     If  the last term has been read from a physical file (i.e.  not from
  1986.     the file user or  a string), unify _F_i_l_e with an absolute path to the
  1987.     file  and _L_i_n_e with the  line-number in the file.   New code  should
  1988.     use pprroolloogg__llooaadd__ccoonntteexxtt//22.
  1989.  
  1990.  
  1991. tteerrmm__eexxppaannssiioonn((_+_T_e_r_m_1_, _-_T_e_r_m_2))
  1992.     Dynamic  predicate, normally not defined.  When defined by  the user
  1993.     all  terms read during consulting that are given to  this predicate.
  1994.     If  the predicate succeeds Prolog will assert _T_e_r_m_2 in  the database
  1995.     rather  then the read term  (_T_e_r_m_1).  _T_e_r_m_2 may  be a term of a  the
  1996.     form  `?- _G_o_a_l' or `:- _G_o_a_l'.  _G_o_a_l is then treated  as a directive.
  1997.     If _T_e_r_m_2 is a  list all terms of the list are stored in the database
  1998.     or  called (for directives).   If  _T_e_r_m_2 is of  the form below,  the
  1999.     system  will assert _C_l_a_u_s_e and record the  indicated source-location
  2000.     with it.
  2001.  
  2002.          '$source_location'(<_F_i_l_e>, <_L_i_n_e>):<_C_l_a_u_s_e>
  2003.  
  2004.     When compiling a  module (see chapter 4 and the directive mmoodduullee//22),
  2005.     eexxppaanndd__tteerrmm//22 will  first try  tteerrmm__eexxppaannssiioonn//22in  the module  being
  2006.     compiled  to  allow  for  term-expansion rules  that  are  local  to
  2007.     a  module.     If  there  is  no  local  definition,  or  the  local
  2008.     definition  fails  to  translate the  term,  eexxppaanndd__tteerrmm//22 will  try
  2009.     user:tteerrmm__eexxppaannssiioonn//22.   For compatibility with SICStus and  Quintus
  2010.     Prolog, this feature should not be used.  See also eexxppaanndd__tteerrmm//22.
  2011.  
  2012.  
  2013. eexxppaanndd__tteerrmm((_+_T_e_r_m_1_, _-_T_e_r_m_2))
  2014.     This  predicate  is  normally  called by  the  compiler  to  perform
  2015.     preprocessing.   First it calls tteerrmm__eexxppaannssiioonn//22.   If this predicate
  2016.     fails  it performs  a grammar-rule translation.    If this fails  it
  2017.     returns the first argument.
  2018.  
  2019.  
  2020. aatt__iinniittiiaalliizzaattiioonn((_+_G_o_a_l))
  2021.     Register   _G_o_a_l   to   be   ran   when   the   system   initialises.
  2022.     Initialisation  takes place after  reloading a .qlf (formerly  .wic)
  2023.     file  as well as after reloading a  saved-state.  The hooks  are run
  2024.     in  the order they were registered.  A warning message is  issued if
  2025.     _G_o_a_l fails, but execution continues.  See also aatt__hhaalltt//11
  2026.  
  2027.  
  2028. aatt__hhaalltt((_+_G_o_a_l))
  2029.     Register  _G_o_a_l to be ran when the  system halts.  The hooks  are run
  2030.     in  the order they were registered.  Success or failure  executing a
  2031.     hook is ignored.  These hooks may not call hhaalltt//[[00,,11]].
  2032.  
  2033.  
  2034. iinniittiiaalliizzaattiioonn((_+_G_o_a_l))
  2035.     Call  _G_o_a_l and  register it  using aatt__iinniittiiaalliizzaattiioonn//11.    Directives
  2036.     that  do  other things  that  creating clauses,  records,  flags  or
  2037.     setting  predicate attributes should normally be written  using this
  2038.     tag  to ensure the  initialisation is executed  when a saved  system
  2039.     starts.  See also qqssaavvee__pprrooggrraamm//[[11,,22]].
  2040.  
  2041.  
  2042. ccoommppiilliinngg
  2043.     Succeeds if the  system is compiling source files with the -c option
  2044.     into  an  intermediate code  file.    Can be  used to  perform  code
  2045.     optimisations in eexxppaanndd__tteerrmm//22 under this condition.
  2046.  
  2047.  
  2048. pprreepprroocceessssoorr((_-_O_l_d_, _+_N_e_w))
  2049.     Read  the input file via a  Unix process that acts as  preprocessor.
  2050.     A  preprocessor is specified  as an atom.   The first occurrence  of
  2051.     the  string `%f' is replaced by the  name of the file to  be loaded.
  2052.     The  resulting atom  is called as  a Unix  command and the  standard
  2053.     output  of this command is loaded.   To use the Unix  C preprocessor
  2054.     one should define:
  2055.  
  2056.     ?- preprocessor(Old, '/lib/cpp -C -P %f'), consult(...).
  2057.  
  2058.     Old = none
  2059.  
  2060.  
  2061. 33..22..11 QQuuiicckk LLooaadd FFiilleess
  2062.  
  2063. The features described in this section should be regarded aallpphhaa.
  2064.  
  2065. As of  version 2.0.0, SWI-Prolog  supports compilation of individual  or
  2066. multiple Prolog  sourcefiles into  `Quick Load  Files'.   A `Quick  Load
  2067. Files' (.qlf  file) stores  the contents  of the file  in a  precompiled
  2068. format very similar to compiled files created using the  -b and -c flags
  2069. (see section 2.7).
  2070.  
  2071. These files load  considerably faster than sourcefiles and are  normally
  2072. more compact.    They are  machine independent  and may  thus be  loaded
  2073. on any  implementation of  SWI-Prolog.   Note however  that clauses  are
  2074. stored as virtual  machine instructions.   Changes to the compiler  will
  2075. generally make old compiled files unusable.
  2076.  
  2077. Quick Load  Files are  created using  qqccoommppiillee//11.   They  may be  loaded
  2078. explicitly using  qqllooaadd//11 or implicitly  using ccoonnssuulltt//11  or one of  the
  2079. other file-loading predicates described  in section 3.2.  If  consult is
  2080. given the  explicit .pl  file, it  will load  the Prolog source.    When
  2081. given the .qlf  file, it will  call qqllooaadd//11 to load the  file.  When  no
  2082. extension is specified, it will load the .qlf file  when present and the
  2083. fileextpl file otherwise.
  2084.  
  2085.  
  2086. qqccoommppiillee((_+_F_i_l_e))
  2087.     Takes  a  single file  specification  like ccoonnssuulltt//11  (i.e.  accepts
  2088.     constructs like library(LibFile)  and creates a Quick Load File from
  2089.     _F_i_l_e.   The file-extension of this file  is .qlf.  The base  name of
  2090.     the Quick Load File is the same as the input file.
  2091.  
  2092.     If   the   file   contains   `:- consult(+File)'   or   `:- [+File]'
  2093.     statements,  the  referred files  are compiled  into  the same  .qlf
  2094.     file.    Other  directives  will  be stored  in  the .qlf  file  and
  2095.     executed in the same fashion as when loading the .pl file.
  2096.  
  2097.     For  tteerrmm__eexxppaannssiioonn//22,  the same  rules as described  in section  2.7
  2098.     apply.
  2099.  
  2100.     Source  references (ssoouurrccee__ffiillee//22)  in the Quick  Load File refer  to
  2101.     the Prolog source file from which the compiled code originates.
  2102.  
  2103.  
  2104. qqllooaadd((_+_F_i_l_e))
  2105.     Loads  the  `Quick  Load  File'.    It  has the  same  semantics  as
  2106.     ccoonnssuulltt//11 for a  normal sourcefile.  Equivalent to consult(File) iff
  2107.     _F_i_l_e refers to a `Quick Load File'.
  2108.  
  2109.  
  2110. 33..33 LLiissttiinngg PPrreeddiiccaatteess aanndd EEddiittoorr IInntteerrffaaccee
  2111.  
  2112. SWI-Prolog offers an interface  to the Unix vi editor and the  GNU Emacs
  2113. invocations emacs and emacsclient.   Which editor is used  is determined
  2114. by the  Unix environment  variable EDITOR,  which should  hold the  full
  2115. pathname of the editor.  If this variable is not defined, vi is used.
  2116.  
  2117. After  the user  quits  the editor,  mmaakkee//00  is  invoked to  reload  all
  2118. modified source files using  ccoonnssuulltt//11.  If the editor can be  quit such
  2119. that  an exit  status non-equal  to 0  is returned  mmaakkee//00  will not  be
  2120. invoked.  _t_o_p can do this by typing control-C, _v_i cannot do this.
  2121.  
  2122. A predicate  specification is either  a term with  the same functor  and
  2123. arity  as the  predicate wanted,  a term  of the  form _F_u_n_c_t_o_r/_A_r_i_t_y  or
  2124. a single  atom.   In  the latter  case the  database is  searched for  a
  2125. predicate of  this name  and arbitrary arity  (see ccuurrrreenntt__pprreeddiiccaattee//22).
  2126. When  more  than  one such  predicate  exists  the  system  will  prompt
  2127. for  confirmation  on  each of  the  matched  predicates.     Predicates
  2128. specifications  are  given   to  the  `Do  What  I  Mean'   system  (see
  2129. ddwwiimm__pprreeddiiccaattee//22) if the requested predicate does not exist.
  2130.  
  2131.  
  2132. eedd((_+_P_r_e_d))
  2133.     Invoke  the user's  preferred  editor on  the source  file of  _P_r_e_d,
  2134.     providing  a search specification  which searches for the  predicate
  2135.     at the start of a line.
  2136.  
  2137.  
  2138. eedd
  2139.     Invoke eedd//11 on the  predicate last edited using eedd//11.  Asks the user
  2140.     to confirm before starting the editor.
  2141.  
  2142.  
  2143. eeddiitt((_+_F_i_l_e))
  2144.     Invoke  the  user's preferred  editor  on  _F_i_l_e.    _F_i_l_e is  a  file
  2145.     specification  as for ccoonnssuulltt//11  (but not  a list).   Note that  the
  2146.     file should exist.
  2147.  
  2148.  
  2149. eeddiitt
  2150.     Invoke  eeddiitt//11 on the file last edited using eeddiitt//11.  Asks  the user
  2151.     to confirm before starting the editor.
  2152.  
  2153.  
  2154. lliissttiinngg((_+_P_r_e_d))
  2155.     List  specified predicates  (when an  atom is  given all  predicates
  2156.     with  this name will  be listed).   The listing  is produced on  the
  2157.     basis  of the  internal representation,  thus loosing user's  layout
  2158.     and variable name information.  See also ppoorrttrraayy__ccllaauussee//11.
  2159.  
  2160.  
  2161. lliissttiinngg
  2162.     List all predicates of the database using lliissttiinngg//11.
  2163.  
  2164.  
  2165. ppoorrttrraayy__ccllaauussee((_+_C_l_a_u_s_e))
  2166.     Pretty  print a  clause  as good  as we  can.   A  clause should  be
  2167.     specified  as a  term `<_H_e_a_d> :- <_B_o_d_y>' (put  brackets around it  to
  2168.     avoid  operator  precedence problems).    Facts  are represented  as
  2169.     `<_H_e_a_d> :- true'.
  2170.  
  2171.  
  2172. eeddiitt__ssoouurrccee((_+_S_p_e_c))
  2173.     A  hook that may be  defined in the module  user to specify how  the
  2174.     predicates  eedd//11 and eeddiitt//11  call the editor.   If eedd//11 is  invoking
  2175.     this hook, _S_p_e_c is a term of the format
  2176.  
  2177.          File:LineNo:Name/Arity
  2178.  
  2179.     Where  _F_i_l_e  and _L_i_n_e_N_o  represents the  location  of the  predicate
  2180.     remembered by Prolog, and  _N_a_m_e and _A_r_i_t_y specify the predicate.  If
  2181.     invoked by eeddiitt//11, _S_p_e_c  is an atom denoting the name of the file to
  2182.     be edited.   This hook is defined by the library library(swi_prolog)
  2183.     distributed  with the  XPCE package  for using XPCE  to edit  Prolog
  2184.     files.
  2185.  
  2186.  
  2187. 33..44 VVeerriiffyy TTyyppee ooff aa TTeerrmm
  2188.  
  2189.  
  2190. vvaarr((_+_T_e_r_m))
  2191.     Succeeds if _T_e_r_m currently is a free variable.
  2192.  
  2193.  
  2194. nnoonnvvaarr((_+_T_e_r_m))
  2195.     Succeeds if _T_e_r_m currently is not a free variable.
  2196.  
  2197.  
  2198. iinntteeggeerr((_+_T_e_r_m))
  2199.     Succeeds if _T_e_r_m is bound to an integer.
  2200.  
  2201.  
  2202. ffllooaatt((_+_T_e_r_m))
  2203.     Succeeds if _T_e_r_m is bound to a floating point number.
  2204.  
  2205.  
  2206. nnuummbbeerr((_+_T_e_r_m))
  2207.     Succeeds if _T_e_r_m is bound to an integer or a floating point number.
  2208.  
  2209.  
  2210. aattoomm((_+_T_e_r_m))
  2211.     Succeeds if _T_e_r_m is bound to an atom.
  2212.  
  2213.  
  2214. ssttrriinngg((_+_T_e_r_m))
  2215.     Succeeds if _T_e_r_m is bound to a string.
  2216.  
  2217.  
  2218. aattoommiicc((_+_T_e_r_m))
  2219.     Succeeds  if _T_e_r_m is bound to  an atom, string, integer or  floating
  2220.     point number.
  2221.  
  2222.  
  2223. ccoommppoouunndd((_+_T_e_r_m))
  2224.     Succeeds  if _T_e_r_m is bound to a  compound term.  See  also ffuunnccttoorr//33
  2225.     and =../2.
  2226.  
  2227.  
  2228. ggrroouunndd((_+_T_e_r_m))
  2229.     Succeeds if _T_e_r_m holds no free variables.
  2230.  
  2231.  
  2232. 33..55 CCoommppaarriissoonn aanndd UUnniiffiiccaattiioonn oorr TTeerrmmss
  2233.  
  2234.  
  2235. 33..55..11 SSttaannddaarrdd OOrrddeerr ooff TTeerrmmss
  2236.  
  2237. Comparison and  unification of arbitrary  terms.   Terms are ordered  in
  2238. the so called ``standard order''.  This order is defined as follows:
  2239.  
  2240.  1. _V_a_r_i_a_b_l_e_s <_A_t_o_m_s <_S_t_r_i_n_g_s <_N_u_m_b_e_r_s <_T_e_r_m_s
  2241.  
  2242.  2. _O_l_d _V_a_r_i_a_b_l_e <_N_e_w _V_a_r_i_a_b_l_e
  2243.  
  2244.  3. _A_t_o_m_s are compared alphabetically.
  2245.  
  2246.  4. _S_t_r_i_n_g_s are compared alphabetically.
  2247.  
  2248.  5. _N_u_m_b_e_r_s  are compared  by value.   Integers  and floats are  treated
  2249.     identically.
  2250.  
  2251.  6. _T_e_r_m_s  are first checked on their functor (alphabetically),  then on
  2252.     their  arity and  finally recursively on  their arguments,  leftmost
  2253.     argument first.
  2254.  
  2255.  
  2256. _+_T_e_r_m_1 == _+_T_e_r_m_2
  2257.     Succeeds  if _T_e_r_m_1  is  equivalent to  _T_e_r_m_2.   A  variable is  only
  2258.     identical to a sharing variable.
  2259.  
  2260.  
  2261. _+_T_e_r_m_1 \== _+_T_e_r_m_2
  2262.     Equivalent to \+Term1 == Term2.
  2263.  
  2264.  
  2265. _+_T_e_r_m_1 = _+_T_e_r_m_2
  2266.     Unify _T_e_r_m_1 with _T_e_r_m_2.  Succeeds if the unification succeeds.
  2267.  
  2268.  
  2269. _+_T_e_r_m_1 \= _+_T_e_r_m_2
  2270.     Equivalent to \+Term1 = Term2.
  2271.  
  2272.  
  2273. _+_T_e_r_m_1 =@= _+_T_e_r_m_2
  2274.     Succeeds  if _T_e_r_m_1  is `structurally  equal' to _T_e_r_m_2.    Structural
  2275.     equivalence  is weaker  than equivalence (==//22),  but stronger  than
  2276.     unification  (=//22).  Two terms are structurally equal if  their tree
  2277.     representation  is identical  and they  have the  same `pattern'  of
  2278.     variables.  Examples:
  2279.  
  2280.                a  =@=  A       false
  2281.                A  =@=  B       true
  2282.           x(A,A)  =@=  x(B,C)  false
  2283.           x(A,A)  =@=  x(B,B)  true
  2284.           x(A,B)  =@=  x(C,D)  true
  2285.  
  2286.  
  2287. _+_T_e_r_m_1 \=@= _+_T_e_r_m_2
  2288.     Equivalent to `\+Term1 =@= Term2'.
  2289.  
  2290.  
  2291. _+_T_e_r_m_1 @< _+_T_e_r_m_2
  2292.     Succeeds if _T_e_r_m_1 is before _T_e_r_m_2 in the standard order of terms.
  2293.  
  2294.  
  2295. _+_T_e_r_m_1 @=< _+_T_e_r_m_2
  2296.     Succeeds if both terms  are equal (==//22) or _T_e_r_m_1 is before _T_e_r_m_2 in
  2297.     the standard order of terms.
  2298.  
  2299.  
  2300. _+_T_e_r_m_1 @> _+_T_e_r_m_2
  2301.     Succeeds if _T_e_r_m_1 is after _T_e_r_m_2 in the standard order of terms.
  2302.  
  2303.  
  2304. _+_T_e_r_m_1 @>= _+_T_e_r_m_2
  2305.     Succeeds  if both terms are equal (==//22) or _T_e_r_m_1 is after  _T_e_r_m_2 in
  2306.     the standard order of terms.
  2307.  
  2308.  
  2309. ccoommppaarree((_?_O_r_d_e_r_, _+_T_e_r_m_1_, _+_T_e_r_m_2))
  2310.     Determine or test  the _O_r_d_e_r between two terms in the standard order
  2311.     of terms.  _O_r_d_e_r is one of <, >  or =, with the obvious meaning.
  2312.  
  2313.  
  2314. 33..66 CCoonnttrrooll PPrreeddiiccaatteess
  2315.  
  2316. The predicates of  this section implement control structures.   Normally
  2317. these constructs  are translated  into virtual  machine instructions  by
  2318. the compiler.   It is still  necessary to implement these constructs  as
  2319. true predicates  to support meta-calls,  as demonstrated in the  example
  2320. below.  The  predicate finds all currently defined atoms of  1 character
  2321. long.   Note that  the cut has no  effect when called  via one of  these
  2322. predicates (see !/0).
  2323.  
  2324. one_character_atoms(As) :-
  2325.         findall(A, (current_atom(A), atom_length(A, 1)), As).
  2326.  
  2327.  
  2328. ffaaiill
  2329.     Always  fail.   The  predicate ffaaiill//00  is translated  into a  single
  2330.     virtual machine instruction.
  2331.  
  2332.  
  2333. ttrruuee
  2334.     Always  succeed.  The predicate  ttrruuee//00 is translated into a  single
  2335.     virtual machine instruction.
  2336.  
  2337.  
  2338. rreeppeeaatt
  2339.     Always succeed, provide an infinite number of choice points.
  2340.  
  2341.  
  2342. !
  2343.     Cut.    Discard choice  points of  parent frame  and frames  created
  2344.     after  the parent  frame.   Note  that the  control structures  ;//22,
  2345.     |//22,  ->//22  and \+//11  are normally handled  by the  compiler and  do
  2346.     not  create a frame,  which implies the  cut operates through  these
  2347.     predicates.   Some  examples are given below.   Note the  difference
  2348.     between t3/1 and t4/1.   Also note the effect of ccaallll//11 in t5/0.  As
  2349.     the  argument of ccaallll//11 is  evaluated by predicates rather than  the
  2350.     compiler the cut has no effect.
  2351.  
  2352.        t1 :- (a, !, fail ; b).        % cuts a/0 and t1/0
  2353.        t2 :- (a -> b, !  ; c).        % cuts b/0 and t2/0
  2354.        t3(G) :- a, G, fail.           % if `G = !'  cuts a/0 and t1/1
  2355.        t4(G) :- a, call(G), fail.     % if `G = !'  cut has no effect
  2356.        t5 :- call((a, !, fail ; b)).  % Cut has no effect
  2357.  
  2358.        t6 :- \+(a, !, fail ; b).      % cuts a/0 and t6/0
  2359.  
  2360.  
  2361. _+_G_o_a_l_1 , _+_G_o_a_l_2
  2362.     Conjunction.   Succeeds if both  `Goal1' and `Goal2' can be  proved.
  2363.     It  is defined as (this  definition does not lead  to a loop as  the
  2364.     second comma is handled by the compiler):
  2365.  
  2366.     Goal1, Goal2 :- Goal1, Goal2.
  2367.  
  2368.  
  2369. _+_G_o_a_l_1 ; _+_G_o_a_l_2
  2370.     The `or' predicate is defined as:
  2371.  
  2372.     Goal1 ; _Goal2 :- Goal1.
  2373.     _Goal1 ; Goal2 :- Goal2.
  2374.  
  2375.  
  2376. _+_G_o_a_l_1 | _+_G_o_a_l_2
  2377.     Equivalent  to ;//22.    Retained for compatibility  only.   New  code
  2378.     should use ;//22.  Still nice though for grammar rules.
  2379.  
  2380.  
  2381. _+_C_o_n_d_i_t_i_o_n -> _+_A_c_t_i_o_n
  2382.     If-then  and  If-Then-Else.    The  ->//22  construct commits  to  the
  2383.     choices  made  at  its  left-hand  side,   destroying  choice-points
  2384.     created  inside the  clause (by  ;//22), or  by goals  called by  this
  2385.     clause.   Unlike !//00,  the choicepoint of  the predicate as a  whole
  2386.     (due  to multiple clauses)  is nnoott destroyed.   The combination  ;//22
  2387.     and ->//22  is defines as:
  2388.  
  2389.     If -> Then; _Else :- If, !, Then.
  2390.     If -> _Then; Else :- !, Else.
  2391.     If -> Then :- If, !, Then.
  2392.  
  2393.     Note   that  the   operator  precedence  relation   between  ;   and
  2394.     ->   ensure  If -> Then ; Else  is  actually  a  term  of  the  form
  2395.     ;(->(If, Then), Else).     The  first  two  clauses  belong  to  the
  2396.     definition of ;//22), while only the last defines ->//22 .
  2397.  
  2398.  
  2399. _+_C_o_n_d_i_t_i_o_n *-> _+_A_c_t_i_o_n _; _+_E_l_s_e
  2400.     This  construct implements  the so-called `soft-cut'.   The  control
  2401.     is  defined as follows:   If _C_o_n_d_i_t_i_o_n succeeds  at least once,  the
  2402.     semantics  is the same  as (_C_o_n_d_i_t_i_o_n, _A_c_t_i_o_n).   If _C_o_n_d_i_t_i_o_n  does
  2403.     not  succeed, the semantics is that of (_C_o_n_d_i_t_i_o_n, _E_l_s_e).   In other
  2404.     words,  If _C_o_n_d_i_t_i_o_n succeeds  at least once,  simply behave as  the
  2405.     conjunction of _C_o_n_d_i_t_i_o_n and _A_c_t_i_o_n, otherwise execute _E_l_s_e.
  2406.  
  2407.  
  2408. \+ _+_G_o_a_l
  2409.     Succeeds  if  `Goal'  cannot  be  proven (mnemonic:    +  refers  to
  2410.     _p_r_o_v_a_b_l_e  and  the  backslash  (\)  is  normally  used  to  indicate
  2411.     negation).
  2412.  
  2413.  
  2414. 33..77 MMeettaa--CCaallll PPrreeddiiccaatteess
  2415.  
  2416. Meta call  predicates are used  to call terms  constructed at run  time.
  2417. The basic meta-call mechanism offered by SWI-Prolog is  to use variables
  2418. as a  subclause (which  should of  course be bound  to a  valid goal  at
  2419. runtime).   A  meta-call is  slower than a  normal call  as it  involves
  2420. actually searching the database at runtime for the  predicate, while for
  2421. normal calls this search is done at compile time.
  2422.  
  2423.  
  2424. ccaallll((_+_G_o_a_l))
  2425.     Invoke  _G_o_a_l as a  goal.   Note that clauses  may have variables  as
  2426.     subclauses,  which is identical to ccaallll//11, except when  the argument
  2427.     is bound to the cut.  See !//00.
  2428.  
  2429.  
  2430. ccaallll((_+_G_o_a_l_, _+_E_x_t_r_a_A_r_g_1_, _._._.))
  2431.     Append  _E_x_t_r_a_A_r_g_1_, _E_x_t_r_a_A_r_g_2_,  _._._.   to  the argument  list of  _G_o_a_l
  2432.     and  call the result.   For  example, call(plus(1), 2, X) will  call
  2433.     pplluuss//33, binding _X to 3.
  2434.  
  2435.     The call/[2..]   construct is handled by the compiler, which implies
  2436.     that  redefinition as  a predicate has  no effect.   The  predicates
  2437.     ccaallll//[[22--66]]  are defined as true  predicates, so they can be  handled
  2438.     by interpreted code.
  2439.  
  2440.  
  2441. aappppllyy((_+_T_e_r_m_, _+_L_i_s_t))
  2442.     Append  the members of  _L_i_s_t to the arguments  of _T_e_r_m and call  the
  2443.     resulting  term.   For  example:   apply(plus(1), [2, X]) will  call
  2444.     plus(1, 2, X).   aappppllyy//22 is  incorporated in the virtual machine  of
  2445.     SWI-Prolog.   This implies that the overhead can be compared  to the
  2446.     overhead  of ccaallll//11.  New code should use call/[2..]   if the length
  2447.     of _L_i_s_t is  fixed, which is more widely supported and faster because
  2448.     there is no need to build and examine the argument list.
  2449.  
  2450.  
  2451. nnoott((_+_G_o_a_l))
  2452.     Succeeds  when _G_o_a_l cannot  be proven.   Retained for  compatibility
  2453.     only.  New code should use \+//11.
  2454.  
  2455.  
  2456. oonnccee((_+_G_o_a_l))
  2457.     Defined as:
  2458.  
  2459.     once(Goal) :-
  2460.             Goal, !.
  2461.  
  2462.     oonnccee//11  can  in  many  cases  be replaced  with  ->//22.     The  only
  2463.     difference  is how the  cut behaves  (see !/0).   The following  two
  2464.     clauses are identical:
  2465.  
  2466.     1) a :- once((b, c)), d.
  2467.     2) a :- b, c -> d.
  2468.  
  2469.  
  2470. iiggnnoorree((_+_G_o_a_l))
  2471.     Calls  _G_o_a_l  as oonnccee//11,  but succeeds,  regardless  of whether  _G_o_a_l
  2472.     succeeded or not.  Defined as:
  2473.  
  2474.     ignore(Goal) :-
  2475.             Goal, !.
  2476.     ignore(_).
  2477.  
  2478.  
  2479. ccaallll__wwiitthh__ddeepptthh__lliimmiitt((_+_G_o_a_l_, _+_L_i_m_i_t_, _-_R_e_s_u_l_t))
  2480.     If  _G_o_a_l can be proven  without recursion deeper than _L_i_m_i_t  levels,
  2481.     ccaallll__wwiitthh__ddeepptthh__lliimmiitt//33 succeeds,  binding  _R_e_s_u_l_t  to  the  deepest
  2482.     recursion  level  used  during the  proof.    Otherwise,  _R_e_s_u_l_t  is
  2483.     unified  with depth_limit_exceeded  if the limit was exceeded  during
  2484.     the  proof,  or the  entire predicate  fails if  _G_o_a_l fails  without
  2485.     exceeding _L_i_m_i_t.
  2486.  
  2487.     The  depth-limit is guarded by the internal machinery.   This differ
  2488.     from  the  depth  computed  based  on a  theoretical  model.     For
  2489.     example,  ttrruuee//00  is  translated  into an  inlined  virtual  machine
  2490.     instruction.   Also, rreeppeeaatt//00 is not implemented as below, but  as a
  2491.     non-deterministic foreign predicate.
  2492.  
  2493.     repeat.
  2494.     repeat :-
  2495.             repeat.
  2496.  
  2497.     As  a  result,  ccaallll__wwiitthh__ddeepptthh__lliimmiitt//33may still  loop  inifitly  on
  2498.     programs  that should  theoretically finish  in finite time.    This
  2499.     problem  can be cured by  using Prolog equivalents to such  built-in
  2500.     predicates.
  2501.  
  2502.     This   predicate  may  be   used  for  theorem-provers  to   realise
  2503.     techniques  like _i_t_e_r_r_a_t_i_v_e  _d_e_e_p_e_n_i_n_g.   It  was implemented  after
  2504.     discussion with Steve Moyle smoyle@ermine.ox.ac.uk.
  2505.  
  2506.  
  2507. 33..88 IISSOO ccoommpplliiaanntt EExxcceeppttiioonn hhaannddlliinngg
  2508.  
  2509. SWI-Prolog defines the predicates ccaattcchh//33 and tthhrrooww//11  for ISO compliant
  2510. raising  and catching  of exceptions.    In  the current  implementation
  2511. (2.9.0), only part of  the built-in predicates generate exceptions.   In
  2512. general, exceptions are implemented for I/O and arithmetic.
  2513.  
  2514.  
  2515. ccaattcchh((_:_G_o_a_l_, _+_C_a_t_c_h_e_r_, _:_R_e_c_o_v_e_r))
  2516.     Behaves  as ccaallll//11 if  no exception is  raised when executing  _G_o_a_l.
  2517.     If  a exception  is raised  using tthhrrooww//11 while  _G_o_a_l executes,  and
  2518.     the  _G_o_a_l is the innermost goal  for which _C_a_t_c_h_e_r unifies with  the
  2519.     argument  of tthhrrooww//11,  all choicepoints generated  by _G_o_a_l are  cut,
  2520.     and _R_e_c_o_v_e_r is called as in ccaallll//11.
  2521.  
  2522.     The  overhead of calling a  goal through ccaattcchh//33 is very  comparable
  2523.     to ccaallll//11.  Recovery from an exception has a similar overhead.
  2524.  
  2525.  
  2526. tthhrrooww((_+_E_x_c_e_p_t_i_o_n))
  2527.     Raise an exception.   The system will look for the innermost ccaattcchh//33
  2528.     ancestor  for which _E_x_c_e_p_t_i_o_n unifies  with the _C_a_t_c_h_e_r argument  of
  2529.     the ccaattcchh//33 call.  See ccaattcchh//33 for details.
  2530.  
  2531.     If  there is no  ccaattcchh//33 willing to catch  the error in the  current
  2532.     Prolog  context,  the  toplevel  (pprroolloogg//00) catches  the  error  and
  2533.     prints  a  warning  message.    If  an  exception was  raised  in  a
  2534.     callback  from C  (see chapter 5),  PPLL__nneexxtt__ssoolluuttiioonn(())will fail  and
  2535.     the exception context can be retrieved using PPLL__eexxcceeppttiioonn(()).
  2536.  
  2537.  
  2538. 33..88..11 DDeebbuuggggiinngg aanndd eexxcceeppttiioonnss
  2539.  
  2540. Before the introduction of exceptions in SWI-Prolog a  runtime error was
  2541. handled by printing an error message, after which  the predicate failed.
  2542. If the feature  (see ffeeaattuurree//22) debug_on_error was in effect  (default),
  2543. the tracer was  switched on.  The  combination of the error message  and
  2544. trace information is generally sufficient to locate the error.
  2545.  
  2546. With exception handling,  things are different.   A programmer may  wish
  2547. to trap an  exception using ccaattcchh//33 to avoid  it reaching the user.   If
  2548. the  exception is  not handled  by user-code,  the interactive  toplevel
  2549. will trap it to prevent termination.
  2550.  
  2551. If  we  do  not  take  special  precautions,   the  context  information
  2552. associated with  an unexpected exception (i.e.  a programming error)  is
  2553. lost.  Therefore,  if an exception is raised, which is not  caught using
  2554. ccaattcchh//33 and the toplevel is running, the error will  be printed, and the
  2555. system will enter trace mode.
  2556.  
  2557. If the system  is in an non-interactive  callback from foreign code  and
  2558. there is no ccaattcchh//33  active in the current context, it  cannot determine
  2559. whether or  not the  exception will  be caught by  the external  routine
  2560. calling  Prolog.    It  will then  base  its  behaviour on  the  feature
  2561. debug_on_error:
  2562.  
  2563.   o _f_e_a_t_u_r_e_(_d_e_b_u_g___o_n___e_r_r_o_r_, _f_a_l_s_e_)
  2564.     The  exception does  not trap the  debugger and  is returned to  the
  2565.     foreign  routine  calling Prolog,  where it  can  be accessed  using
  2566.     PPLL__eexxcceeppttiioonn(()).  This is the default.
  2567.  
  2568.   o _f_e_a_t_u_r_e_(_d_e_b_u_g___o_n___e_r_r_o_r_, _t_r_u_e_)
  2569.     If the exception is  not caught by Prolog in the current context, it
  2570.     will trap the tracer to help analysing the context of the error.
  2571.  
  2572. While looking for the  context in which an exception takes place,  it is
  2573. adviced to switch on debug mode using the predicate ddeebbuugg//00.
  2574.  
  2575.  
  2576. 33..88..22 TThhee eexxcceeppttiioonn tteerrmm
  2577.  
  2578. Builtin  predicates  generates exceptions  using  a  term  error(_F_o_r_m_a_l_,
  2579. _C_o_n_t_e_x_t).    The  first argument  is  the  `formal' description  of  the
  2580. error,  specifying the class  and generic  defined context  information.
  2581. When applicable,  the ISO  error-term definition  is used.   The  second
  2582. part  describes some  additional context  to help  the programmer  while
  2583. debugging.    In its  most  generic form  this  is a  term of  the  form
  2584. context(_N_a_m_e_/_A_r_i_t_y_,  _M_e_s_s_a_g_e), where _N_a_m_e/_A_r_i_t_y  describes the  built-in
  2585. predicate  that raised  the error,  and _M_e_s_s_a_g_e  provides an  additional
  2586. description of the error.  Any part of this structure  may be a variable
  2587. if no information was present.
  2588.  
  2589.  
  2590. 33..88..33 PPrriinnttiinngg aa mmeessssaaggee ffrroomm aann eexxcceeppttiioonn
  2591.  
  2592. The predicate pprriinntt__mmeessssaaggee//22 may be used to print an exception  term in
  2593. a human readable format:
  2594.  
  2595.  
  2596. pprriinntt__mmeessssaaggee((_+_K_i_n_d_, _+_T_e_r_m))
  2597.     This  predicate is  modelled after  the Quintus  predicate with  the
  2598.     same  name, though  its current  implementation is incomplete.    It
  2599.     is  used only  for printing messages  from exceptions from  built-in
  2600.     predicates.    _K_i_n_d is  one of  informational, warning,  consterror,
  2601.     help  or silent.    Currently only  error is defined.    _T_e_r_m is  an
  2602.     eerrrroorr((_2)) term described  in section 3.8.2.  A human-readable message
  2603.     is printed to the stream user_error.
  2604.  
  2605.     This  predicate first  obtains the `human  translation' of _T_e_r_m  and
  2606.     then  calls mmeessssaaggee__hhooookk//33.  If this fails the message  is printed to
  2607.     the stream user_error.
  2608.  
  2609.     The  pprriinntt__mmeessssaaggee//22  predicate  and  its  rules  are  in  the  file
  2610.     <_p_l_h_o_m_e>/boot/messages.pl,   which  may   be   inspected  for   more
  2611.     information on the error messages and related error terms.
  2612.  
  2613.  
  2614. mmeessssaaggee__hhooookk((_+_T_e_r_m_, _+_K_i_n_d_, _+_M_e_s_s_a_g_e))
  2615.     Hook  predicate that may be define  in the module user to  intercept
  2616.     messages  from  pprriinntt__mmeessssaaggee//22.    _T_e_r_m  and _K_i_n_d  are the  same  as
  2617.     passed  to  pprriinntt__mmeessssaaggee//22.    _M_e_s_s_a_g_e is  a  string containing  the
  2618.     human  readable  translation of  the  message.   If  this  predicate
  2619.     succeeds, pprriinntt__mmeessssaaggee//22 considers the message printed.
  2620.  
  2621.     This  predicate should  be defined  dynamic and  multifile to  allow
  2622.     other modules defining clauses for it too.
  2623.  
  2624.  
  2625. 33..99 AAddvvaanncceedd ccoonnttrrooll--ssttrruuccttuurreess::  bblloocckkss
  2626.  
  2627. The predicates of this section form a tightly related  set for realising
  2628. premature successful or  failing exits from a  _b_l_o_c_k.  These  predicates
  2629. are  first of  all  useful for  error-recovery.    They  were  primarily
  2630. implemented for compatibility reasons.
  2631.  
  2632.  
  2633. bblloocckk((_+_L_a_b_e_l_, _+_G_o_a_l_, _-_E_x_i_t_V_a_l_u_e))
  2634.     Execute  _G_o_a_l in a _b_l_o_c_k.   _L_a_b_e_l is the name  of the block.   _L_a_b_e_l
  2635.     is  normally an  atom, but  the system imposes  no type  constraints
  2636.     and  may even be a variable.   _E_x_i_t_V_a_l_u_e is normally unified  to the
  2637.     second argument of an eexxiitt//22 call invoked by _G_o_a_l.
  2638.  
  2639.  
  2640. eexxiitt((_+_L_a_b_e_l_, _+_V_a_l_u_e))
  2641.     Calling  eexxiitt//22 makes the innermost _b_l_o_c_k which _L_a_b_e_l  unifies exit.
  2642.     The  block's _E_x_i_t_V_a_l_u_e is unified with  _V_a_l_u_e.  If this  unification
  2643.     fails the block fails.
  2644.  
  2645.  
  2646. ffaaiill((_+_L_a_b_e_l))
  2647.     Calling  ffaaiill//11 makes the innermost  _b_l_o_c_k which _L_a_b_e_l unifies  fail
  2648.     immediately.  Implemented as
  2649.  
  2650.     fail(Label) :- !(Label), fail.
  2651.  
  2652.  
  2653. !((_+_L_a_b_e_l))
  2654.     Cut  all  choice-points created  since the  entry  of the  innermost
  2655.     _b_l_o_c_k which _L_a_b_e_l unifies.
  2656.  
  2657. The example  below illustrate these constructs  to immediately report  a
  2658. syntax-error from a  `deep-down' procedure to the outside world  without
  2659. passing it as an argument `all-over-the-place'.
  2660.  
  2661. parse(RuleSet, InputList, Rest) :-
  2662.         block(syntaxerror, phrase(RuleSet, InputList, Rest), Error),
  2663.         (   var(Error)
  2664.         ->  true
  2665.         ;   format('Syntax-error: ~w~n', Error),
  2666.             fail
  2667.         ).
  2668.  
  2669. integer(N) -->
  2670.         digit(D1), !, digits(Ds),
  2671.         { name(N, [D1|Ds]) }.
  2672.  
  2673. digits([D|R]) --> digit(D), digits(R).
  2674. digits(_) --> letter(_), !, { exit(syntaxerror, 'Illegal number') }.
  2675. digits([]) --> [].
  2676.  
  2677. digit(D, [D|R], R)  :- between(0'0, 0'9, D).
  2678. letter(D, [D|R], R) :- between(0'a, 0'z, D).
  2679.  
  2680.  
  2681. 33..1100 GGrraammmmaarr rruullee iinntteerrffaaccee ((pphhrraassee))
  2682.  
  2683. The predicates below may be called to activate a grammar-rule set:
  2684.  
  2685.  
  2686. pphhrraassee((_+_R_u_l_e_S_e_t_, _+_I_n_p_u_t_L_i_s_t))
  2687.     Equivalent to phrase(RuleSet, InputList, []).
  2688.  
  2689.  
  2690. pphhrraassee((_+_R_u_l_e_S_e_t_, _+_I_n_p_u_t_L_i_s_t_, _-_R_e_s_t))
  2691.     Activate  the rule-set with given name.  `InputList' is the  list of
  2692.     tokens to parse,  `Rest' is unified with the remaining tokens if the
  2693.     sentence is parsed correctly.
  2694.  
  2695.  
  2696. 33..1111 DDaattaabbaassee
  2697.  
  2698. SWI-Prolog offers  three different database mechanisms.   The first  one
  2699. is  the common  assert/retract  mechanism  for manipulating  the  clause
  2700. database.    As facts  and clauses  asserted using  aasssseerrtt//11  or one  of
  2701. its  derivatives become  part of  the program  these predicates  compile
  2702. the term  given to them.    rreettrraacctt//11 and rreettrraaccttaallll//11  have to unify  a
  2703. term and  therefore have to  decompile the program.   For these  reasons
  2704. the assert/retract  mechanism is  expensive.   On the  other hand,  once
  2705. compiled, queries to the database are faster than  querying the recorded
  2706. database discussed below.  See also ddyynnaammiicc//11.
  2707.  
  2708. The second way of  storing arbitrary terms in the database is  using the
  2709. ``recorded database''.   In  this database terms  are associated with  a
  2710. _k_e_y.  A key can be an atom, integer or term.   In the last case only the
  2711. functor and  arity determine the  key.   Each key has  a chain of  terms
  2712. associated with it.   New terms  can be added either  at the head or  at
  2713. the tail  of this  chain.   This mechanism  is considerably faster  than
  2714. the assert/retract mechanism as terms are not compiled,  but just copied
  2715. into the heap.
  2716.  
  2717. The third mechanism is a special purpose one.   It associates an integer
  2718. or atom  with a  key,  which is  an atom,  integer or  term.   Each  key
  2719. can only  have one  atom or integer  associated with  it.   It again  is
  2720. considerably faster  than the mechanisms described  above, but can  only
  2721. be used to store simple status information like counters, etc.
  2722.  
  2723.  
  2724. aabboolliisshh((_:_P_r_e_d_i_c_a_t_e_I_n_d_i_c_a_t_o_r))
  2725.     Removes  all clauses of a  predicate with functor _F_u_n_c_t_o_r and  arity
  2726.     _A_r_i_t_y  from  the  database.    Unlike  version  1.2,  all  predicate
  2727.     attributes  (dynamic, multifile, index,  etc.)   are reset to  their
  2728.     defaults.   Abolishing an imported predicate only removes the import
  2729.     link;  the predicate will keep its old definition in  its definition
  2730.     module.    For `cleanup'  of the  dynamic database,  one should  use
  2731.     rreettrraaccttaallll//11 rather than aabboolliisshh//22.
  2732.  
  2733.  
  2734. aabboolliisshh((_+_N_a_m_e_, _+_A_r_i_t_y))
  2735.     Same  as abolish(Name/Arity).   The predicate aabboolliisshh//22 conforms  to
  2736.     the Edinburgh standard, while aabboolliisshh//11 is ISO compliant.
  2737.  
  2738.  
  2739. rreeddeeffiinnee__ssyysstteemm__pprreeddiiccaattee((_+_H_e_a_d))
  2740.     This  directive  may be  used  both in  module  user and  in  normal
  2741.     modules to redefine  any system predicate.  If the system definition
  2742.     is  redefined in  module user,  the  new definition  is the  default
  2743.     definition  for  all sub-modules.    Otherwise  the redefinition  is
  2744.     local  to the module.   The system definition remains in the  module
  2745.     system.
  2746.  
  2747.     Redefining   system   predicate   facilitates  the   definition   of
  2748.     compatibility packages.  Use in other context is discouraged.
  2749.  
  2750.  
  2751. rreettrraacctt((_+_T_e_r_m))
  2752.     When  _T_e_r_m  is an  atom  or a  term  it is  unified with  the  first
  2753.     unifying  fact or clause  in the database.   The  fact or clause  is
  2754.     removed from the database.
  2755.  
  2756.  
  2757. rreettrraaccttaallll((_+_H_e_a_d))
  2758.     All  facts or  clauses in the  database for  which the _h_e_a_d  unifies
  2759.     with _H_e_a_d are removed.
  2760.  
  2761.  
  2762. aasssseerrtt((_+_T_e_r_m))
  2763.     Assert  a fact or clause in the  database.  _T_e_r_m is asserted  as the
  2764.     last fact or clause of the corresponding predicate.
  2765.  
  2766.  
  2767. aasssseerrttaa((_+_T_e_r_m))
  2768.     Equivalent  to aasssseerrtt//11,  but _T_e_r_m  is asserted as  first clause  or
  2769.     fact of the predicate.
  2770.  
  2771.  
  2772. aasssseerrttzz((_+_T_e_r_m))
  2773.     Equivalent to aasssseerrtt//11.
  2774.  
  2775.  
  2776. aasssseerrtt((_+_T_e_r_m_, _-_R_e_f_e_r_e_n_c_e))
  2777.     Equivalent  to  aasssseerrtt//11, but  _R_e_f_e_r_e_n_c_e is  unified  with a  unique
  2778.     reference  to the asserted clause.  This key can later be  used with
  2779.     ccllaauussee//33 or eerraassee//11.
  2780.  
  2781.  
  2782. aasssseerrttaa((_+_T_e_r_m_, _-_R_e_f_e_r_e_n_c_e))
  2783.     Equivalent  to aasssseerrtt//22,  but _T_e_r_m  is asserted as  first clause  or
  2784.     fact of the predicate.
  2785.  
  2786.  
  2787. aasssseerrttzz((_+_T_e_r_m_, _-_R_e_f_e_r_e_n_c_e))
  2788.     Equivalent to aasssseerrtt//22.
  2789.  
  2790.  
  2791. rreeccoorrddaa((_+_K_e_y_, _+_T_e_r_m_, _-_R_e_f_e_r_e_n_c_e))
  2792.     Assert  _T_e_r_m  in the  recorded  database  under key  _K_e_y.    _K_e_y  is
  2793.     an  integer,  atom or  term.   _R_e_f_e_r_e_n_c_e  is unified  with a  unique
  2794.     reference to the record (see eerraassee//11).
  2795.  
  2796.  
  2797. rreeccoorrddaa((_+_K_e_y_, _+_T_e_r_m))
  2798.     Equivalent to recorda(Key, Value,  _).
  2799.  
  2800.  
  2801. rreeccoorrddzz((_+_K_e_y_, _+_T_e_r_m_, _-_R_e_f_e_r_e_n_c_e))
  2802.     Equivalent to rreeccoorrddaa//33, but  puts the _T_e_r_m at the tail of the terms
  2803.     recorded under _K_e_y.
  2804.  
  2805.  
  2806. rreeccoorrddzz((_+_K_e_y_, _+_T_e_r_m))
  2807.     Equivalent to recordz(Key, Value,  _).
  2808.  
  2809.  
  2810. rreeccoorrddeedd((_+_K_e_y_, _-_V_a_l_u_e_, _-_R_e_f_e_r_e_n_c_e))
  2811.     Unify  _V_a_l_u_e  with the  first  term recorded  under _K_e_y  which  does
  2812.     unify.    _R_e_f_e_r_e_n_c_e  is  unified with  the  memory location  of  the
  2813.     record.
  2814.  
  2815.  
  2816. rreeccoorrddeedd((_+_K_e_y_, _-_V_a_l_u_e))
  2817.     Equivalent to recorded(Key, Value,  _).
  2818.  
  2819.  
  2820. eerraassee((_+_R_e_f_e_r_e_n_c_e))
  2821.     Erase  a  record or  clause from  the  database.   _R_e_f_e_r_e_n_c_e  is  an
  2822.     integer  returned by  rreeccoorrddaa//33 or  rreeccoorrddeedd//33, ccllaauussee//33,  aasssseerrtt//22,
  2823.     aasssseerrttaa//22  or aasssseerrttzz//22.    Other integers might  conflict with  the
  2824.     internal  consistency of the system.  Erase can only be  called once
  2825.     on  a record or clause.  A second call also might  conflict with the
  2826.     internal consistency of the system.
  2827.  
  2828.  
  2829. ffllaagg((_+_K_e_y_, _-_O_l_d_, _+_N_e_w))
  2830.     _K_e_y  is an  atom, integer or  term.   Unify _O_l_d  with the old  value
  2831.     associated  with _K_e_y.  If the key is used for the first  time _O_l_d is
  2832.     unified  with the integer  0.   Then store the  value of _N_e_w,  which
  2833.     should  be an integer, float,  atom or arithmetic expression,  under
  2834.     _K_e_y.   ffllaagg//33 is a very  fast mechanism for storing simple  facts in
  2835.     the database.  Example:
  2836.  
  2837.     :- module_transparent succeeds_n_times/2.
  2838.  
  2839.     succeeds_n_times(Goal, Times) :-
  2840.             flag(succeeds_n_times, Old, 0),
  2841.             Goal,
  2842.             flag(succeeds_n_times, N, N+1),
  2843.             fail ; flag(succeeds_n_times, Times, Old).
  2844.  
  2845.  
  2846. 33..1111..11 IInnddeexxiinngg ddaattaabbaasseess
  2847.  
  2848. By  default,   SWI-Prolog,  as   most  other  implementations,   indexes
  2849. predicates  on their  first argument.    SWI-Prolog  allows indexing  on
  2850. other and multiple arguments using the declaration iinnddeexx//11.
  2851.  
  2852. For advanced database indexing, it defines hhaasshh__tteerrmm//22:
  2853.  
  2854.  
  2855. hhaasshh__tteerrmm((_+_T_e_r_m_, _-_H_a_s_h_K_e_y))
  2856.     If  _T_e_r_m is a ground term (see ggrroouunndd//11), _H_a_s_h_K_e_y is unified  with a
  2857.     positive integer value that  may be used as a hash-key to the value.
  2858.     If  _T_e_r_m is not ground, the predicate succeeds  immediately, leaving
  2859.     _H_a_s_h_K_e_y an unbound variable.
  2860.  
  2861.     This  predicate  may be  used to  build hash-tables  as  well as  to
  2862.     exploit argument-indexing to find complex terms more quickly.
  2863.  
  2864.     The  hash-key does not rely on temporary information  like addresses
  2865.     of  atoms and may be assumed constant over different  invocations of
  2866.     SWI-Prolog.
  2867.  
  2868.  
  2869. 33..1122 DDeeccllaarriinngg PPrrooppeerrttiieess ooff PPrreeddiiccaatteess
  2870.  
  2871. This  section  describes  directives  which  manipulate   attributes  of
  2872. predicate  definitions.     The  functors  ddyynnaammiicc//11,   mmuullttiiffiillee//11  and
  2873. ddiissccoonnttiigguuoouuss//11  are  operators  of  priority  1150  (see  oopp//33),  which
  2874. implies  the  list of  predicates  they  involve  can just  be  a  comma
  2875. separated list:
  2876.  
  2877. :- dynamic
  2878.         foo/0,
  2879.         baz/2.
  2880.  
  2881. On SWI-Prolog all  these directives are just  predicates.  This  implies
  2882. they can also  be called by a program.   Do not rely on this  feature if
  2883. you want to maintain portability to other Prolog implementations.
  2884.  
  2885.  
  2886. ddyynnaammiicc _+_F_u_n_c_t_o_r_/_+_A_r_i_t_y_, _._._.
  2887.     Informs  the interpreter  that  the definition  of the  predicate(s)
  2888.     may  change  during  execution (using  aasssseerrtt//11  and/or  rreettrraacctt//11).
  2889.     Currently  ddyynnaammiicc//11  only stops  the interpreter  from  complaining
  2890.     about  undefined  predicates  (see  uunnkknnoowwnn//22).     Future  releases
  2891.     might  prohibit  aasssseerrtt//11  and rreettrraacctt//11  for  not-dynamic  declared
  2892.     procedures.
  2893.  
  2894.  
  2895. mmuullttiiffiillee _+_F_u_n_c_t_o_r_/_+_A_r_i_t_y_, _._._.
  2896.     Informs  the system that the  specified predicate(s) may be  defined
  2897.     over  more than one  file.  This  stops ccoonnssuulltt//11 from redefining  a
  2898.     predicate when a new definition is found.
  2899.  
  2900.  
  2901. ddiissccoonnttiigguuoouuss _+_F_u_n_c_t_o_r_/_+_A_r_i_t_y_, _._._.
  2902.     Informs  the system that the  clauses of the specified  predicate(s)
  2903.     might not be together in the source file.  See also ssttyyllee__cchheecckk//11.
  2904.  
  2905.  
  2906. iinnddeexx((_+_H_e_a_d))
  2907.     Index  the clauses  of the predicate  with the  same name and  arity
  2908.     as  _H_e_a_d  on the  specified arguments.    _H_e_a_d is  a  term of  which
  2909.     all  arguments  are  either  `1' (denoting  `index  this  argument')
  2910.     or  `0'  (denoting `do  not index  this argument').    Indexing  has
  2911.     no  implications  for the  semantics of  a  predicate, only  on  its
  2912.     performance.    If  indexing is  enabled on  a  predicate a  special
  2913.     purpose  algorithm  is used  to select  candidate  clauses based  on
  2914.     the  actual arguments of  the goal.   This algorithm checks  whether
  2915.     indexed  arguments might  unify in  the clause  head.   Only  atoms,
  2916.     integers  and  functors   (e.g.  name  and  arity  of  a  term)  are
  2917.     considered.    Indexing  is  very useful  for predicates  with  many
  2918.     clauses representing facts.
  2919.  
  2920.     Due to the  representation technique used at most 4 arguments can be
  2921.     indexed.  All  indexed arguments should be in the first 32 arguments
  2922.     of  the predicate.    If  more than  4 arguments  are specified  for
  2923.     indexing only the first  4 will be accepted.  Arguments above 32 are
  2924.     ignored for indexing.
  2925.  
  2926.     By  default  all predicates  with <_a_r_i_t_y> 1  are indexed  on their
  2927.     first  argument.  It is possible to redefine indexing  on predicates
  2928.     that  already have clauses attached to  them.  This will initiate  a
  2929.     scan through the  predicates clause list to update the index summary
  2930.     information stored with each clause.
  2931.  
  2932.     If---for  example---one wants to  represents sub-types using a  fact
  2933.     list  `sub_type(Sub, Super)' that should be  used both to  determine
  2934.     sub- and super types one should declare sub_type/2 as follows:
  2935.  
  2936.     :- index(sub_type(1, 1)).
  2937.  
  2938.     sub_type(horse, animal).
  2939.     ...
  2940.     ...
  2941.  
  2942.  
  2943. 33..1133 EExxaammiinniinngg tthhee PPrrooggrraamm
  2944.  
  2945.  
  2946. ccuurrrreenntt__aattoomm((_-_A_t_o_m))
  2947.     Successively unifies _A_t_o_m with  all atoms known to the system.  Note
  2948.     that  ccuurrrreenntt__aattoomm//11always  succeeds if _A_t_o_m  is instantiated to  an
  2949.     atom.
  2950.  
  2951.  
  2952. ccuurrrreenntt__ffuunnccttoorr((_?_N_a_m_e_, _?_A_r_i_t_y))
  2953.     Successively unifies _N_a_m_e  with the name and _A_r_i_t_y with the arity of
  2954.     functors known to the system.
  2955.  
  2956.  
  2957. ccuurrrreenntt__ffllaagg((_-_F_l_a_g_K_e_y))
  2958.     Successively  unifies  _F_l_a_g_K_e_y with  all keys  used  for flags  (see
  2959.     ffllaagg//33).
  2960.  
  2961.  
  2962. ccuurrrreenntt__kkeeyy((_-_K_e_y))
  2963.     Successively  unifies  _K_e_y  with  all keys  used  for  records  (see
  2964.     rreeccoorrddaa//33, etc.).
  2965.  
  2966.  
  2967. ccuurrrreenntt__pprreeddiiccaattee((_?_N_a_m_e_, _?_H_e_a_d))
  2968.     Successively  unifies _N_a_m_e  with  the name  of predicates  currently
  2969.     defined and _H_e_a_d with  the most general term built from _N_a_m_e and the
  2970.     arity of the  predicate.  This predicate succeeds for all predicates
  2971.     defined  in the specified module, imported  to it, or in one  of the
  2972.     modules from which the predicate will be imported if it is called.
  2973.  
  2974.  
  2975. pprreeddiiccaattee__pprrooppeerrttyy((_?_H_e_a_d_, _?_P_r_o_p_e_r_t_y))
  2976.     Succeeds  if _H_e_a_d refers to a predicate that has  property _P_r_o_p_e_r_t_y.
  2977.     Can  be used  to test whether  a predicate  has a certain  property,
  2978.     obtain  all properties known  for _H_e_a_d,  find all predicates  having
  2979.     _p_r_o_p_e_r_t_y  or  even obtaining  all  information available  about  the
  2980.     current program.  _P_r_o_p_e_r_t_y is one of:
  2981.  
  2982.     iinntteerrpprreetteedd
  2983.          Is true if the predicate is defined in Prolog.   We return true
  2984.          on this because, although the code is actually  compiled, it is
  2985.          completely transparent, just like interpreted code.
  2986.  
  2987.     bbuuiilltt__iinn
  2988.          Is true  if the predicate  is locked  as a built-in  predicate.
  2989.          This implies it  cannot be redefined  in its definition  module
  2990.          and it can normally not be seen in the tracer.
  2991.  
  2992.     ffoorreeiiggnn
  2993.          Is true if the predicate is defined in the C language.
  2994.  
  2995.     ddyynnaammiicc
  2996.          Is  true  if  the  predicate  is  declared  dynamic  using  the
  2997.          ddyynnaammiicc//11 declaration.
  2998.  
  2999.     mmuullttiiffiillee
  3000.          Is  true if  the  predicate  is declared  multifile  using  the
  3001.          mmuullttiiffiillee//11 declaration.
  3002.  
  3003.     uunnddeeffiinneedd
  3004.          Is  true if  a procedure  definition  block for  the  predicate
  3005.          exists, but there are no  clauses in it and it is  not declared
  3006.          dynamic.   This is  true if  the predicate occurs  in the  body
  3007.          of a  loaded predicate,  an attempt  to call it  has been  made
  3008.          via one  of the  meta-call predicates  or the  predicate had  a
  3009.          definition in  the past.   See  the library  package _c_h_e_c_k  for
  3010.          example usage.
  3011.  
  3012.     ttrraannssppaarreenntt
  3013.          Is true  if the  predicate  is declared  transparent using  the
  3014.          mmoodduullee__ttrraannssppaarreenntt//11declaration.
  3015.  
  3016.     eexxppoorrtteedd
  3017.          Is true if the predicate  is in the public list of  the context
  3018.          module.
  3019.  
  3020.     iimmppoorrtteedd__ffrroomm((_M_o_d_u_l_e))
  3021.          Is true if  the predicate is  imported into the context  module
  3022.          from module _M_o_d_u_l_e.
  3023.  
  3024.     iinnddeexxeedd((_H_e_a_d))
  3025.          Predicate is indexed (see iinnddeexx//11) according to _H_e_a_d.   _H_e_a_d is
  3026.          a term  whose name and  arity are  identical to the  predicate.
  3027.          The arguments are unified  with `1' for indexed arguments,  `0'
  3028.          otherwise.
  3029.  
  3030.     ffiillee((_F_i_l_e_N_a_m_e))
  3031.          Unify _F_i_l_e_N_a_m_e  with the name  of the  sourcefile in which  the
  3032.          predicate is defined.  See also ssoouurrccee__ffiillee//22.
  3033.  
  3034.     lliinnee__ccoouunntt((_L_i_n_e_N_u_m_b_e_r))
  3035.          Unify _L_i_n_e_N_u_m_b_e_r with  the line number  of the first clause  of
  3036.          the predicate.   Fails if the predicate is not  associated with
  3037.          a file.  See also ssoouurrccee__ffiillee//22.
  3038.  
  3039.     nnuummbbeerr__ooff__ccllaauusseess((_C_l_a_u_s_e_C_o_u_n_t))
  3040.          Unify _C_l_a_u_s_e_C_o_u_n_t to the number of clauses  associated with the
  3041.          predicate.  Fails for foreign predicates.
  3042.  
  3043.  
  3044. ddwwiimm__pprreeddiiccaattee((_+_T_e_r_m_, _-_D_w_i_m))
  3045.     `Do What I Mean'  (`dwim') support predicate.  _T_e_r_m is a term, which
  3046.     name  and arity  are used  as a predicate  specification.   _D_w_i_m  is
  3047.     instantiated  with the  most general  term built from  _N_a_m_e and  the
  3048.     arity  of a defined predicate  that matches the predicate  specified
  3049.     by  _T_e_r_m in the  `Do What I Mean'  sense.  See  ddwwiimm__mmaattcchh//22for  `Do
  3050.     What  I Mean' string matching.   Internal system predicates are  not
  3051.     generated,  unless  style_check(+dollar) is  active.    Backtracking
  3052.     provides all alternative matches.
  3053.  
  3054.  
  3055. ccllaauussee((_?_H_e_a_d_, _?_B_o_d_y))
  3056.     Succeeds  when  _H_e_a_d can  be unified  with a  clause  head and  _B_o_d_y
  3057.     with  the  corresponding clause  body.    Gives alternative  clauses
  3058.     on  backtracking.     For  facts  _B_o_d_y  is  unified  with  the  atom
  3059.     _t_r_u_e.   Normally ccllaauussee//22 is  used to find clause definitions for  a
  3060.     predicate,  but it can  also be used to  find clause heads for  some
  3061.     body template.
  3062.  
  3063.  
  3064. ccllaauussee((_?_H_e_a_d_, _?_B_o_d_y_, _?_R_e_f_e_r_e_n_c_e))
  3065.     Equivalent  to  ccllaauussee//22,   but  unifies  _R_e_f_e_r_e_n_c_e  with  a  unique
  3066.     reference to the  clause (see also aasssseerrtt//22, eerraassee//11).  If _R_e_f_e_r_e_n_c_e
  3067.     is  instantiated to a reference the  clause's head and body will  be
  3068.     unified with  _H_e_a_d and _B_o_d_y.
  3069.  
  3070.  
  3071. nntthh__ccllaauussee((_?_P_r_e_d_, _?_I_n_d_e_x_, _?_R_e_f_e_r_e_n_c_e))
  3072.     Provides  access to  the clauses  of a predicate  using their  index
  3073.     number.    Counting  starts at  1.   If  _R_e_f_e_r_e_n_c_e  is specified  it
  3074.     unifies  _P_r_e_d with the  most general term  with the same  name/arity
  3075.     as  the predicate  and _I_n_d_e_x  with the index-number  of the  clause.
  3076.     Otherwise  the name  and arity  of _P_r_e_d  are used  to determine  the
  3077.     predicate.    If _I_n_d_e_x is  provided _R_e_f_e_r_e_n_c_e  will be unified  with
  3078.     the  clause  reference.   If  _I_n_d_e_x  is unbound,  backtracking  will
  3079.     yield  both the  indices and the  references of  all clauses of  the
  3080.     predicate.  The following example finds the 2nd clause of mmeemmbbeerr//22:
  3081.  
  3082.     ?- nth_clause(member(_,_), 2, Ref), clause(Head, Body, Ref).
  3083.  
  3084.     Ref = 160088
  3085.     Head = system : member(G575, [G578|G579])
  3086.     Body = member(G575, G579)
  3087.  
  3088.  
  3089. ccllaauussee__pprrooppeerrttyy((_+_C_l_a_u_s_e_R_e_f_, _-_P_r_o_p_e_r_t_y))
  3090.     Queries  properties  of   a  clause.     _C_l_a_u_s_e_R_e_f  is  a  reference
  3091.     to   a   clause   as   produced   by   ccllaauussee//33,   nntthh__ccllaauussee//33   or
  3092.     pprroolloogg__ffrraammee__aattttrriibbuuttee//33.  _P_r_o_p_e_r_t_y is one of the following:
  3093.  
  3094.     ffiillee((_F_i_l_e_N_a_m_e))
  3095.          Unify _F_i_l_e_N_a_m_e  with the name  of the  sourcefile in which  the
  3096.          clause is defined.  Fails if the clause is not  associated to a
  3097.          file.
  3098.  
  3099.     lliinnee__ccoouunntt((_L_i_n_e_N_u_m_b_e_r))
  3100.          Unify _L_i_n_e_N_u_m_b_e_r with the line number of the clause.   Fails if
  3101.          the clause is not associated to a file.
  3102.  
  3103.     ffaacctt
  3104.          True if the clause has no body.
  3105.  
  3106.     eerraasseedd
  3107.          True if  the  clause has  been erased,  but  not yet  reclaimed
  3108.          because it is referenced.
  3109.  
  3110.  
  3111. 33..1144 IInnppuutt aanndd OOuuttppuutt
  3112.  
  3113. SWI-Prolog provides two  different packages for input  and output.   One
  3114. confirms  to  the  Edinburgh  standard.    This  package  has  a  notion
  3115. of  `current-input' and  `current-output'.    The  reading  and  writing
  3116. predicates implicitly refer  to these streams.   In the second  package,
  3117. streams are  opened explicitly and  the resulting handle  is used as  an
  3118. argument to the reading  and writing predicate to specify the  source or
  3119. destination.   Both packages are fully  integrated; the user may  switch
  3120. freely between them.
  3121.  
  3122.  
  3123. 33..1144..11 IInnppuutt aanndd OOuuttppuutt UUssiinngg IImmpplliicciitt SSoouurrccee aanndd DDeessttiinnaattiioonn
  3124.  
  3125. The  package  for  implicit input  and  output  destination  is  upwards
  3126. compatible to DEC-10 and  C-Prolog.  The reading and  writing predicates
  3127. refer to resp.   the current input- and output stream.   Initially these
  3128. streams are  connected to the  terminal.  The  current output stream  is
  3129. changed using tteellll//11 or  aappppeenndd//11.  The current input stream  is changed
  3130. using sseeee//11.  The streams current value can  be obtained using tteelllliinngg//11
  3131. for output- and sseeeeiinngg//11  for input streams.  The table below  shows the
  3132. valid stream specifications.  The reserved names user_input, user_output
  3133. and user_error are for neat integration with the explicit streams.
  3134.  
  3135.            ___________________________________________________
  3136.            | user        |This  reserved name  refers to  the|
  3137.            |             |terminal                           |
  3138.            | user_input  I|nput from the terminal            |
  3139.  
  3140.            | user_output O|utput to the terminal             |
  3141.            | user_error  U|nix error stream (output only)    |
  3142.            | <_A_t_o_m>      N|ame of a Unix file                |
  3143.            |_pipe(<_A_t_o_m>)N|ame_of_a_Unix_command_____________|_
  3144.  
  3145. Source and  destination are  either a file,  one of  the reserved  words
  3146. above, or a term  `pipe(_C_o_m_m_a_n_d)'.  In the predicate  descriptions below
  3147. we will call the source/destination argument `_S_r_c_D_e_s_t'.   Below are some
  3148. examples of source/destination specifications.
  3149.  
  3150.        ?- see(data).        % Start reading from file `data'.
  3151.        ?- tell(stderr).     % Start writing on the error stream.
  3152.  
  3153.        ?- tell(pipe(lpr)).  % Start writing to the printer.
  3154.  
  3155. Another example  of using  the ppiippee//11 construct  is shown  below.   Note
  3156. that  the  ppiippee//11  construct  is  not  part  of  Prolog's  standard  I/O
  3157. repertoire.
  3158.  
  3159. getwd(Wd) :-
  3160.         seeing(Old), see(pipe(pwd)),
  3161.         collect_wd(String),
  3162.         seen, see(Old),
  3163.         atom_chars(Wd, String).
  3164.  
  3165. collect_wd([C|R]) :-
  3166.         get0(C), C \== -1, !,
  3167.         collect_wd(R).
  3168. collect_wd([]).
  3169.  
  3170.  
  3171. sseeee((_+_S_r_c_D_e_s_t))
  3172.     Make  _S_r_c_D_e_s_t the  current input  stream.   If  _S_r_c_D_e_s_t was  already
  3173.     opened  for  reading  with sseeee//11  and  has  not been  closed  since,
  3174.     reading  will be resumed.  Otherwise _S_r_c_D_e_s_t will be opened  and the
  3175.     file pointer is positioned at the start of the file.
  3176.  
  3177.  
  3178. tteellll((_+_S_r_c_D_e_s_t))
  3179.     Make  _S_r_c_D_e_s_t the  current output stream.    If _S_r_c_D_e_s_t was  already
  3180.     opened  for writing with tteellll//11 or aappppeenndd//11 and has not  been closed
  3181.     since,  writing will  be resumed.    Otherwise the  file is  created
  3182.     or---when existing---truncated.  See also aappppeenndd//11.
  3183.  
  3184.  
  3185. aappppeenndd((_+_F_i_l_e))
  3186.     Similar  to tteellll//11,  but positions the  file pointer  at the end  of
  3187.     _F_i_l_e  rather than truncating an existing  file.  The pipe  construct
  3188.     is not accepted by this predicate.
  3189.  
  3190.  
  3191. sseeeeiinngg((_?_S_r_c_D_e_s_t))
  3192.     Unify the name of the current input stream with _S_r_c_D_e_s_t.
  3193.  
  3194.  
  3195. tteelllliinngg((_?_S_r_c_D_e_s_t))
  3196.     Unify the name of the current output stream with _S_r_c_D_e_s_t.
  3197.  
  3198.  
  3199. sseeeenn
  3200.     Close the current input stream.  The new input stream becomes _u_s_e_r.
  3201.  
  3202.  
  3203. ttoolldd
  3204.     Close  the current  output stream.   The  new output stream  becomes
  3205.     _u_s_e_r.
  3206.  
  3207.  
  3208. 33..1144..22 EExxpplliicciitt IInnppuutt aanndd OOuuttppuutt SSttrreeaammss
  3209.  
  3210. The predicates  below are  part of the  Quintus compatible  stream-based
  3211. I/O package.   In this package streams are explicitly created  using the
  3212. predicate ooppeenn//33.   The resulting stream identifier is then passed  as a
  3213. parameter to  the reading and writing  predicates to specify the  source
  3214. or destination of the data.
  3215.  
  3216.  
  3217. ooppeenn((_+_S_r_c_D_e_s_t_, _+_M_o_d_e_, _-_S_t_r_e_a_m_, _+_O_p_t_i_o_n_s))
  3218.     ISO  compliant predicate  to open  a stream.   _S_r_c_D_e_s  is either  an
  3219.     atom,  specifying  a  Unix file,  or  a term  `pipe(Command)',  just
  3220.     like  sseeee//11 and  tteellll//11.   _M_o_d_e  is one  of read,  write, append  or
  3221.     update.   Mode  append opens the file  for writing, positioning  the
  3222.     file-pointer  at the end.   Mode update opens the file for  writing,
  3223.     positioning  the file-pointer at the  beginning of the file  without
  3224.     truncating  the file.  See also ssttrreeaamm__ppoossiittiioonn//33.   _S_t_r_e_a_m is either
  3225.     a variable, in which  case it is bound to an integer identifying the
  3226.     stream,  or an  atom, in  which case  this atom will  be the  stream
  3227.     identifier.  The _O_p_t_i_o_n_s list can contain the following options:
  3228.  
  3229.     ttyyppee((_T_y_p_e))
  3230.          Using type text (default), Prolog will write a  text-file in an
  3231.          operating-system compatible way.   Using type binary the  bytes
  3232.          will be read  or written without any  translation.  Note  there
  3233.          is no difference between the two on Unix systems.
  3234.  
  3235.     aalliiaass((_A_t_o_m))
  3236.          Gives  the  stream  a name.     The  following  two  calls  are
  3237.          identical, but only the latter is allowed in ISO Prolog.
  3238.  
  3239.          ?- open(foo, read, in, []).
  3240.          ?- open(foo, read, S, [alias(in)]).
  3241.  
  3242.     eeooff__aaccttiioonn((_A_c_t_i_o_n))
  3243.          Defines  what  happens  if the  end  of  the  input  stream  is
  3244.          reached.   Action eof_code makes ggeett00//11  and friends return  -1
  3245.          and rreeaadd//11 and friends return the atom end_of_file.  Repetitive
  3246.          reading keeps yielding the  same result.  Action error  is like
  3247.          eof_code, but repetitive  reading will  raise an error.    With
  3248.          action reset,  Prolog will  examine the file  again and  return
  3249.          more data if the file has grown.
  3250.  
  3251.     bbuuffffeerr((_B_u_f_f_e_r_i_n_g))
  3252.          Defines output  buffering.   The atom  fullf (default)  defines
  3253.          full buffering, line buffering  by line, and false implies  the
  3254.          stream is  fully unbuffered.   Smaller  buffering is useful  if
  3255.          another process or the user is waiting for the output  as it is
  3256.          being produced.   See  also fflluusshh//00  and fflluusshh__oouuttppuutt//11.    This
  3257.          option is not an ISO option.
  3258.  
  3259.     cclloossee__oonn__aabboorrtt((_B_o_o_l))
  3260.          If  true (default),  the  stream is  closed  on an  abort  (see
  3261.          aabboorrtt//00).   If  false, the  stream is  not closed.    If it  is
  3262.          an output  stream,  it will  be flushed  however.   Useful  for
  3263.          logfiles and if  the stream is  associated to a process  (using
  3264.          the ppiippee//11 construct).
  3265.  
  3266.     The  option reposition is not supported in SWI-Prolog.   All streams
  3267.     connected to a file may be repositioned.
  3268.  
  3269.  
  3270. ooppeenn((_+_S_r_c_D_e_s_t_, _+_M_o_d_e_, _?_S_t_r_e_a_m))
  3271.     Equivalent to ooppeenn//44 with an empty option-list.
  3272.  
  3273.  
  3274. ooppeenn__nnuullll__ssttrreeaamm((_?_S_t_r_e_a_m))
  3275.     Open  a stream that produces no output.  All counting  functions are
  3276.     enabled  on such a stream.   An attempt  to read from a  null-stream
  3277.     will  immediately signal  end-of-file.   Similar to Unix  /dev/null.
  3278.     _S_t_r_e_a_m can be an atom, giving the null-stream an alias name.
  3279.  
  3280.  
  3281. cclloossee((_+_S_t_r_e_a_m))
  3282.  
  3283.     Close the specified stream.   If _S_t_r_e_a_m is not open an error message
  3284.     is  displayed.  If the closed stream is the current input  or output
  3285.     stream the terminal is made the current input or output.
  3286.  
  3287.  
  3288. ccuurrrreenntt__ssttrreeaamm((_?_F_i_l_e_, _?_M_o_d_e_, _?_S_t_r_e_a_m))
  3289.     Is  true if  a stream with  file specification _F_i_l_e,  mode _M_o_d_e  and
  3290.     stream  identifier _S_t_r_e_a_m is  open.   The reserved streams user  and
  3291.     user_error are  not generated by  this predicate.   If a stream  has
  3292.     been  opened  with mode  append this  predicate  will generate  mode
  3293.     write.
  3294.  
  3295.  
  3296. ssttrreeaamm__ppoossiittiioonn((_+_S_t_r_e_a_m_, _-_O_l_d_, _+_N_e_w))
  3297.     Unify  the position parameters  of _S_t_r_e_a_m with  _O_l_d and set them  to
  3298.     _N_e_w.  A position is represented by the following term:
  3299.  
  3300.     '$stream_position'(CharNo, LineNo, LinePos).
  3301.  
  3302.     It is only  possible to change the position parameters if the stream
  3303.     is  connected to  a disk  file.   If  the position  is changed,  the
  3304.     _C_h_a_r_N_o  field determines the new position  in the file.  The  _L_i_n_e_N_o
  3305.     and _L_i_n_e_P_o_s are copied in the stream administration.
  3306.  
  3307.  
  3308. 33..1144..33 SSwwiittcchhiinngg BBeettwweeeenn IImmpplliicciitt aanndd EExxpplliicciitt II//OO
  3309.  
  3310. The predicates  below can be  used for  switching between the  implicit-
  3311. and the explicit stream based I/O predicates.
  3312.  
  3313.  
  3314. sseett__iinnppuutt((_+_S_t_r_e_a_m))
  3315.     Set  the current input  stream to become _S_t_r_e_a_m.   Thus,  open(file,
  3316.     read, Stream), set_input(Stream) is equivalent to see(file).
  3317.  
  3318.  
  3319. sseett__oouuttppuutt((_+_S_t_r_e_a_m))
  3320.     Set the current output stream to become _S_t_r_e_a_m.
  3321.  
  3322.  
  3323. ccuurrrreenntt__iinnppuutt((_-_S_t_r_e_a_m))
  3324.     Get  the current input stream.   Useful to get access to  the status
  3325.     predicates associated with streams.
  3326.  
  3327.  
  3328. ccuurrrreenntt__oouuttppuutt((_-_S_t_r_e_a_m))
  3329.     Get the current output stream.
  3330.  
  3331.  
  3332. dduupp__ssttrreeaamm((_+_F_r_o_m_, _+_T_o))
  3333.     Duplicate  the  underlying data  from stream  _F_r_o_m to  streamTo,  so
  3334.     actions  performed  on either  stream have  the same  effect.    The
  3335.     primary  goal  of this  predicate is  to  facilitate redirection  of
  3336.     the  user  interaction  to allow  for  `interactor'  windows.    For
  3337.     example,  the following code will redirect output to user_output and
  3338.     user_error to an XPCE text window:
  3339.  
  3340.             ...,
  3341.             pce_open(Window, append, Fd),
  3342.             dup_stream(user_output, Fd),
  3343.             dup_stream(user_error, Fd),
  3344.             ...
  3345.  
  3346.     The  old  status of  a stream  can  be stored  by duplicating  to  a
  3347.     null-stream as obtained using ooppeenn__nnuullll__ssttrreeaamm//11.
  3348.  
  3349.     This predicate is SWI-Prolog specific.
  3350.  
  3351.  
  3352. 33..1155 SSttaattuuss ooff IInnppuutt aanndd OOuuttppuutt SSttrreeaammss
  3353.  
  3354.  
  3355. wwaaiitt__ffoorr__iinnppuutt((_+_L_i_s_t_O_f_S_t_r_e_a_m_s_, _-_R_e_a_d_y_L_i_s_t_, _+_T_i_m_e_O_u_t))
  3356.     Wait  for input on  one of the  streams in _L_i_s_t_O_f_S_t_r_e_a_m_s and  return
  3357.     a  list  of  streams  on  which input  is  available  in  _R_e_a_d_y_L_i_s_t.
  3358.     wwaaiitt__ffoorr__iinnppuutt//33 waits for  at most _T_i_m_e_O_u_t  seconds.   _T_i_m_e_o_u_t  may
  3359.     be  specified as a floating point  number to specify fractions of  a
  3360.     second.   If  _T_i_m_e_o_u_t equals 0, wwaaiitt__ffoorr__iinnppuutt//33waits  indefinitely.
  3361.     This  predicate can be used  to implement timeout while reading  and
  3362.     to  handle  input from  multiple  sources.   The  following  example
  3363.     will  wait for input from the  user and an explicitly opened  second
  3364.     terminal.  On return, _I_n_p_u_t_s may hold user or _P_4 or both.
  3365.  
  3366.     ?- open('/dev/ttyp4', read, P4),
  3367.        wait_for_input([user, P4], Inputs, 0).
  3368.  
  3369.  
  3370. cchhaarraacctteerr__ccoouunntt((_+_S_t_r_e_a_m_, _-_C_o_u_n_t))
  3371.     Unify  _C_o_u_n_t with the  current character index.   For input  streams
  3372.     this  is the number  of characters read since  the open, for  output
  3373.     streams  this is the number of characters written.   Counting starts
  3374.     at 0.
  3375.  
  3376.  
  3377. lliinnee__ccoouunntt((_+_S_t_r_e_a_m_, _-_C_o_u_n_t))
  3378.     Unify  _C_o_u_n_t with the  number of  lines read or  written.   Counting
  3379.     starts at 1.
  3380.  
  3381.  
  3382. lliinnee__ppoossiittiioonn((_+_S_t_r_e_a_m_, _-_C_o_u_n_t))
  3383.     Unify  _C_o_u_n_t with the position on the current line.  Note  that this
  3384.     assumes  the position is 0 after the  open.  Tabs are assumed  to be
  3385.     defined on each  8-th character and backspaces are assumed to reduce
  3386.     the count by one, provided it is positive.
  3387.  
  3388.  
  3389. ffiilleeeerrrroorrss((_-_O_l_d_, _+_N_e_w))
  3390.     Define error behaviour  on errors when opening a file for reading or
  3391.     writing.   Valid values are the  atoms on (default) and off.   First
  3392.     _O_l_d  is unified with the current value.   Then the new value  is set
  3393.     to _N_e_w.
  3394.  
  3395.  
  3396. 33..1166 PPrriimmiittiivvee CChhaarraacctteerr IInnppuutt aanndd OOuuttppuutt
  3397.  
  3398.  
  3399. nnll
  3400.     Write  a newline character  to the current output  stream.  On  Unix
  3401.     systems nnll//00 is equivalent to put(10).
  3402.  
  3403.  
  3404. nnll((_+_S_t_r_e_a_m))
  3405.     Write a newline to _S_t_r_e_a_m.
  3406.  
  3407.  
  3408. ppuutt((_+_C_h_a_r))
  3409.     Write  _C_h_a_r  to  the  current  output  stream,  _C_h_a_r  is  either  an
  3410.     integer-expression  evaluating to an ASCII  value (0 _C_h_a_r 255) or
  3411.     an atom of one character.
  3412.  
  3413.  
  3414. ppuutt((_+_S_t_r_e_a_m_, _+_C_h_a_r))
  3415.     Write _C_h_a_r to _S_t_r_e_a_m.
  3416.  
  3417.  
  3418. ttaabb((_+_A_m_o_u_n_t))
  3419.     Writes  _A_m_o_u_n_t  spaces  on  the  current  output  stream.     _A_m_o_u_n_t
  3420.     should  be an expression that  evaluates to a positive integer  (see
  3421.     section 3.22).
  3422.  
  3423.  
  3424. ttaabb((_+_S_t_r_e_a_m_, _+_A_m_o_u_n_t))
  3425.     Writes _A_m_o_u_n_t spaces to _S_t_r_e_a_m.
  3426.  
  3427.  
  3428. fflluusshh
  3429.     Flush  pending  output  on  current  output  stream.     fflluusshh//00  is
  3430.     automatically  generated by  rreeaadd//11 and derivatives  if the  current
  3431.     input stream is user and the cursor is not at the left margin.
  3432.  
  3433.  
  3434. fflluusshh__oouuttppuutt((_+_S_t_r_e_a_m))
  3435.     Flush  output on the specified stream.  The stream must be  open for
  3436.     writing.
  3437.  
  3438.  
  3439. ttttyyfflluusshh
  3440.     Flush pending output on stream _u_s_e_r.  See also fflluusshh//00.
  3441.  
  3442.  
  3443. ggeett00((_-_C_h_a_r))
  3444.     Read  the current  input stream  and unify the  next character  with
  3445.     _C_h_a_r.  _C_h_a_r is unified with -1 on end of file.
  3446.  
  3447.  
  3448. ggeett00((_+_S_t_r_e_a_m_, _-_C_h_a_r))
  3449.     Read the next character from _S_t_r_e_a_m.
  3450.  
  3451.  
  3452. ggeett((_-_C_h_a_r))
  3453.     Read  the  current   input  stream  and  unify  the  next  non-blank
  3454.     character with _C_h_a_r.  _C_h_a_r is unified with -1 on end of file.
  3455.  
  3456.  
  3457. ggeett((_+_S_t_r_e_a_m_, _-_C_h_a_r))
  3458.     Read the next non-blank character from _S_t_r_e_a_m.
  3459.  
  3460.  
  3461. ppeeeekk__bbyyttee((_-_C_h_a_r))
  3462.     Reads  the next input character like ggeett00//11, but does not  remove it
  3463.     from the input stream.  This predicate is ISO compliant.
  3464.  
  3465.  
  3466. ppeeeekk__bbyyttee((_+_S_t_r_e_a_m_, _-_C_h_a_r))
  3467.     Reads  the next input character like ggeett00//22, but does not  remove it
  3468.     from the stream.  This predicate is ISO compliant.
  3469.  
  3470.  
  3471. sskkiipp((_+_C_h_a_r))
  3472.     Read the input until  _C_h_a_r or the end of the file is encountered.  A
  3473.     subsequent call to ggeett00//11 will read the first character after _C_h_a_r.
  3474.  
  3475.  
  3476. sskkiipp((_+_S_t_r_e_a_m_, _+_C_h_a_r))
  3477.     Skip input (as sskkiipp//11) on _S_t_r_e_a_m.
  3478.  
  3479.  
  3480. ggeett__ssiinnggllee__cchhaarr((_-_C_h_a_r))
  3481.     Get  a single character from input stream `user' (regardless  of the
  3482.     current  input stream).  Unlike ggeett00//11 this predicate does  not wait
  3483.     for  a return.  The character is not echoed to the  user's terminal.
  3484.     This  predicate  is meant  for keyboard  menu selection  etc..    If
  3485.     SWI-Prolog was started  with the -tty option this predicate reads an
  3486.     entire  line of input and  returns the first non-blank character  on
  3487.     this line, or the  ASCII code of the newline (10) if the entire line
  3488.     consisted of blank characters.
  3489.  
  3490.  
  3491. aatt__eenndd__ooff__ssttrreeaamm
  3492.     Succeeds  after the last character  of the current input stream  has
  3493.     been  read.    Also succeeds  if  there is  no valid  current  input
  3494.     stream.
  3495.  
  3496.  
  3497. aatt__eenndd__ooff__ssttrreeaamm((_+_S_t_r_e_a_m))
  3498.     Succeeds  after the last character of  the named stream is read,  or
  3499.     _S_t_r_e_a_m is not a valid input stream.
  3500.  
  3501.  
  3502. 33..1177 TTeerrmm RReeaaddiinngg aanndd WWrriittiinngg
  3503.  
  3504. This section  describes the basic term  reading and writing  predicates.
  3505. The  predicates  tteerrmm__ttoo__aattoomm//22 and  aattoomm__ttoo__tteerrmm//33 provide   means  for
  3506. translating atoms  and strings to  terms.   The predicates  ffoorrmmaatt//[[11,,22]]
  3507. and wwrriitteeff//22 provide formatted output.
  3508.  
  3509. There are  two ways  to manipulate  the output  format.   The  predicate
  3510. pprriinntt//[[11,,22]] may be programmed  using ppoorrttrraayy//11.  The format  of floating
  3511. point  numbers may  be  manipulated using  the feature  (see  ffeeaattuurree//22)
  3512. float_format.
  3513.  
  3514. Reading is  sensitive to the  feature character_escapes, which  controls
  3515. the interpretation of the \ character in quoted atoms and strings.
  3516.  
  3517.  
  3518. wwrriittee__tteerrmm((_+_T_e_r_m_, _+_O_p_t_i_o_n_s))
  3519.     The  predicate  wwrriittee__tteerrmm//22 is  the  generic  form  of  all  Prolog
  3520.     term-write predicates.  Valid options are:
  3521.  
  3522.     qquuootteedd((true _o_r false))
  3523.          If true, atoms and  functors that needs quotes will  be quoted.
  3524.          The default is false.
  3525.  
  3526.     iiggnnoorree__ooppss((true _o_r false))
  3527.          If true, the generic term-representation (<_f_u_n_c_t_o_r>(<_a_r_g_s> ...))
  3528.          will be  used for  all terms,  Otherwise (default),  operators,
  3529.          list-notation and  {}/1  will be  written using  their  special
  3530.          syntax.
  3531.  
  3532.     nnuummbbeerrvvaarrss((true _o_r false))
  3533.          If true, terms  of the format $VAR(N), where  <_N> is a positive
  3534.          integer, will be  written as a variable  name.  The default  is
  3535.          false.
  3536.  
  3537.     ppoorrttrraayy((true _o_r false))
  3538.          If true, the  hook ppoorrttrraayy//11 is  called before printing a  term
  3539.          that is not  a variable.   If ppoorrttrraayy//11  succeeds, the term  is
  3540.          considered printed.  See  also pprriinntt//11.  The default  is false.
  3541.          This option is an extension to the ISO write_term options.
  3542.  
  3543.  
  3544. wwrriittee__tteerrmm((_+_S_t_r_e_a_m_, _+_T_e_r_m_, _+_O_p_t_i_o_n_s))
  3545.     As  wwrriittee__tteerrmm//22,  but  output  is sent  to  _S_t_r_e_a_m rather  than  the
  3546.     current output.
  3547.  
  3548.  
  3549. wwrriittee__ccaannoonniiccaall((_+_T_e_r_m))
  3550.     Write  _T_e_r_m on  the current output  stream using standard  parenthe-
  3551.     sised prefix notation  (i.e. ignoring operator declarations).  Atoms
  3552.     that need quotes are  quoted.  Terms written with this predicate can
  3553.     always  be read back,  regardless of current operator  declarations.
  3554.     Equivalent to wwrriittee__tteerrmm//22 using the options ignore_ops and quoted.
  3555.  
  3556.  
  3557. wwrriittee__ccaannoonniiccaall((_+_S_t_r_e_a_m_, _+_T_e_r_m))
  3558.     Write _T_e_r_m in canonical form on _S_t_r_e_a_m.
  3559.  
  3560.  
  3561. wwrriittee((_+_T_e_r_m))
  3562.     Write  _T_e_r_m  to the  current output,  using  brackets and  operators
  3563.     where  appropriate.   See ffeeaattuurree//22  for controlling floating  point
  3564.     output format.
  3565.  
  3566.  
  3567. wwrriittee((_+_S_t_r_e_a_m_, _+_T_e_r_m))
  3568.     Write _T_e_r_m to _S_t_r_e_a_m.
  3569.  
  3570.  
  3571. wwrriitteeqq((_+_T_e_r_m))
  3572.     Write  _T_e_r_m  to the  current output,  using  brackets and  operators
  3573.     where  appropriate.    Atoms that  need quotes  are quoted.    Terms
  3574.     written  with this predicate can  be read back with rreeaadd//11  provided
  3575.     the currently active operator declarations are identical.
  3576.  
  3577.  
  3578. wwrriitteeqq((_+_S_t_r_e_a_m_, _+_T_e_r_m))
  3579.     Write _T_e_r_m to _S_t_r_e_a_m, inserting quotes.
  3580.  
  3581.  
  3582. pprriinntt((_+_T_e_r_m))
  3583.     Prints  _T_e_r_m on the  current output stream  similar to wwrriittee//11,  but
  3584.     for each (sub)term  of _T_e_r_m first the dynamic predicate ppoorrttrraayy//11 is
  3585.     called.   If this predicate succeeds _p_r_i_n_t assumes the (sub)term has
  3586.     been written.  This allows for user defined term writing.
  3587.  
  3588.  
  3589. pprriinntt((_+_S_t_r_e_a_m_, _+_T_e_r_m))
  3590.     Print _T_e_r_m to _S_t_r_e_a_m.
  3591.  
  3592.  
  3593. ppoorrttrraayy((_+_T_e_r_m))
  3594.     A dynamic predicate, which  can be defined by the user to change the
  3595.     behaviour  of pprriinntt//11 on (sub)terms.   For each subterm  encountered
  3596.     that is not  a variable pprriinntt//11 first calls ppoorrttrraayy//11 using the term
  3597.     as  argument.    For lists  only the  list as  a whole  is given  to
  3598.     ppoorrttrraayy//11.   If portray succeeds  pprriinntt//11 assumes the term has  been
  3599.     written.
  3600.  
  3601.  
  3602. rreeaadd((_-_T_e_r_m))
  3603.     Read  the next Prolog term from  the current input stream and  unify
  3604.     it  with _T_e_r_m.  On a syntax error rreeaadd//11 displays an  error message,
  3605.     attempts  to  skip  the erroneous  term  and  fails.    On  reaching
  3606.     end-of-file _T_e_r_m is unified with the atom end_of_file.
  3607.  
  3608.  
  3609. rreeaadd((_+_S_t_r_e_a_m_, _-_T_e_r_m))
  3610.     Read _T_e_r_m from _S_t_r_e_a_m.
  3611.  
  3612.  
  3613. rreeaadd__ccllaauussee((_-_T_e_r_m))
  3614.     Equivalent  to  rreeaadd//11,  but  warns  the  user  for  variables  only
  3615.     occurring  once in a term  (singleton variables) which do not  start
  3616.     with  an underscore  if style_check(singleton) is active  (default).
  3617.     Used  to read Prolog source files (see ccoonnssuulltt//11).  New  code should
  3618.     use rreeaadd__tteerrmm//22 with the option singletons(warning).
  3619.  
  3620.  
  3621. rreeaadd__ccllaauussee((_+_S_t_r_e_a_m_, _-_T_e_r_m))
  3622.     Read a clause from _S_t_r_e_a_m.  See rreeaadd__ccllaauussee//11.
  3623.  
  3624.  
  3625. rreeaadd__vvaarriiaabblleess((_-_T_e_r_m_, _-_B_i_n_d_i_n_g_s))
  3626.     Similar  to  rreeaadd//11,   but  _B_i_n_d_i_n_g_s  is  unified  with  a  list  of
  3627.     `_N_a_m_e = _V_a_r' tuples, thus  providing access to  the actual variable
  3628.     names.      New  code  should   use  rreeaadd__tteerrmm//22 using  the   option
  3629.     variables(X).
  3630.  
  3631.  
  3632. rreeaadd__vvaarriiaabblleess((_+_S_t_r_e_a_m_, _-_T_e_r_m_, _-_B_i_n_d_i_n_g_s))
  3633.     Read,    returning  term   and   bindings   from  _S_t_r_e_a_m.        See
  3634.     rreeaadd__vvaarriiaabblleess//22.
  3635.  
  3636.  
  3637. rreeaadd__tteerrmm((_-_T_e_r_m_, _+_O_p_t_i_o_n_s))
  3638.     Read  a term from the current  input stream and unify the term  with
  3639.     _T_e_r_m.    The  reading is  controlled  by options  from the  list  of
  3640.     _O_p_t_i_o_n_s.   If this list is  empty, the behaviour is the same  as for
  3641.     rreeaadd//11.   The options are upward compatible to Quintus Prolog.   The
  3642.     argument order is according to the ISO standard.  Options:
  3643.  
  3644.     ssyynnttaaxx__eerrrroorrss((_a_t_o_m _o_r _v_a_r_i_a_b_l_e))
  3645.          Define the  behaviour for  when  a syntax  error occurs.    The
  3646.          possible values are:
  3647.  
  3648.          ffaaiill
  3649.              Default behaviour.  The error  is reported as a warning and
  3650.              the predicate fails.
  3651.  
  3652.          qquuiieett
  3653.              Quietly fails if a syntax error has occurred.
  3654.  
  3655.          VVaarriiaabbllee
  3656.              If  no error  occurs, the  variable is  unified with  none,
  3657.              otherwise _V_a_r_i_a_b_l_e is unified with a term of the form
  3658.  
  3659.              '$stream_position'(CharNo, LineNo, LinePos):Message
  3660.  
  3661.              This behaviour is a SWI-Prolog extension.
  3662.  
  3663.     vvaarriiaabbllee__nnaammeess((_V_a_r_s))
  3664.          Unify _V_a_r_s with a list  of `_N_a_m_e = _V_a_r', where _N_a_m_e is  an atom
  3665.          describing the variable name and _V_a_r is a  variable that shares
  3666.          with the corresponding variable in _T_e_r_m.
  3667.  
  3668.     ssiinngglleettoonnss((_V_a_r_s))
  3669.          As variable_names,  but only  reports  the variables  occurring
  3670.          only  once in  the  _T_e_r_m read.     Variables starting  with  an
  3671.          underscore (`_') are not included in this list.
  3672.  
  3673.     tteerrmm__ppoossiittiioonn((_P_o_s))
  3674.          Unifies _P_o_s with the starting  position of the term read.   _P_o_s
  3675.          if of the same format as use by ssttrreeaamm__ppoossiittiioonn//33.
  3676.  
  3677.     ssuubbtteerrmm__ppoossiittiioonnss((_T_e_r_m_P_o_s))
  3678.          Describes the  detailed layout of  the term.   The formats  for
  3679.          the various types of terms  if given below.  All  positions are
  3680.          character positions.    If  the input  is related  to a  normal
  3681.          stream,  these  positions are  relative  to the  start  of  the
  3682.          input, when  reading from  the terminal, they  are relative  to
  3683.          the start of the term.
  3684.  
  3685.          FFrroomm--TToo
  3686.              Used for primitive types (atoms, numbers, variables).
  3687.  
  3688.          ssttrriinngg__ppoossiittiioonn((_F_r_o_m_, _T_o))
  3689.              Used  to indicate  the  position of  a string  enclosed  in
  3690.              double quotes (").
  3691.  
  3692.          bbrraaccee__tteerrmm__ppoossiittiioonn((_F_r_o_m_, _T_o_, _A_r_g))
  3693.              Term  of  the form  {...},  as used  in  DCG  rules.    _A_r_g
  3694.              describes the argument.
  3695.  
  3696.          lliisstt__ppoossiittiioonn((_F_r_o_m_, _T_o_, _E_l_m_s_, _T_a_i_l))
  3697.              A list.  _E_l_m_s describes the  positions of the elements.  If
  3698.              the list specifies the tail as |<_T_a_i_l_T_e_r_m>, _T_a_i_l is unified
  3699.              with  the term-position  of the  tail,  otherwise with  the
  3700.              atom none.
  3701.  
  3702.          tteerrmm__ppoossiittiioonn((_F_r_o_m_, _T_o_, _F_F_r_o_m_, _F_T_o_, _S_u_b_P_o_s))
  3703.              Used  for a compound  term not matching  one of the  above.
  3704.              _F_F_r_o_m  and  _F_T_o  describe  the  position  of  the  functor.
  3705.              _S_u_b_P_o_s  is a  list,  each element  of  which describes  the
  3706.              term-position of the corresponding subterm.
  3707.  
  3708.  
  3709. rreeaadd__tteerrmm((_+_S_t_r_e_a_m_, _-_T_e_r_m_, _+_O_p_t_i_o_n_s))
  3710.     Read term with options from _S_t_r_e_a_m.  See rreeaadd__tteerrmm//22.
  3711.  
  3712.  
  3713. rreeaadd__hhiissttoorryy((_+_S_h_o_w_, _+_H_e_l_p_, _+_S_p_e_c_i_a_l_, _+_P_r_o_m_p_t_, _-_T_e_r_m_, _-_B_i_n_d_i_n_g_s))
  3714.     Similar  to rreeaadd__vvaarriiaabblleess//22,  but allows for history  substitutions.
  3715.     rreeaadd__hhiissttoorryy//66 is used by the top level to read  the user's actions.
  3716.     _S_h_o_w  is the command the user should type to show the  saved events.
  3717.     _H_e_l_p  is  the  command  to  get an  overview  of  the  capabilities.
  3718.     _S_p_e_c_i_a_l  is a list  of commands that are  not saved in the  history.
  3719.     _P_r_o_m_p_t  is  the  first  prompt  given.    Continuation  prompts  for
  3720.     more  lines are  determined by  pprroommpptt//22.   A  %w in  the prompt  is
  3721.     substituted  by the  event number.   See  section 2.4 for  available
  3722.     substitutions.
  3723.  
  3724.     SWI-Prolog calls rreeaadd__hhiissttoorryy//66 as follows:
  3725.  
  3726.     read_history(h, '!h', [trace], '%w ?- ', Goal, Bindings)
  3727.  
  3728.  
  3729. pprroommpptt((_-_O_l_d_, _+_N_e_w))
  3730.     Set  prompt associated  with rreeaadd//11  and its  derivatives.   _O_l_d  is
  3731.     first  unified with the current prompt.  On success the  prompt will
  3732.     be  set to _N_e_w if  this is an atom.   Otherwise an error message  is
  3733.     displayed.   A prompt  is printed if one  of the read predicates  is
  3734.     called  and the cursor is  at the left margin.   It is also  printed
  3735.     whenever  a newline is given and  the term has not been  terminated.
  3736.     Prompts are only printed when the current input stream is _u_s_e_r.
  3737.  
  3738.  
  3739. pprroommpptt11((_+_P_r_o_m_p_t))
  3740.     Sets  the prompt for the next line  to be read.   Continuation lines
  3741.     will be read using the prompt defined by pprroommpptt//22.
  3742.  
  3743.  
  3744. 33..1188 AAnnaallyyssiinngg aanndd CCoonnssttrruuccttiinngg TTeerrmmss
  3745.  
  3746.  
  3747. ffuunnccttoorr((_?_T_e_r_m_, _?_F_u_n_c_t_o_r_, _?_A_r_i_t_y))
  3748.     Succeeds  if _T_e_r_m is  a term with  functor _F_u_n_c_t_o_r and arity  _A_r_i_t_y.
  3749.     If  _T_e_r_m is a variable  it is unified with  a new term holding  only
  3750.     variables.    ffuunnccttoorr//33 silently  fails on  instantiation faults  If
  3751.     _T_e_r_m  is an atom or  number, _F_u_n_c_t_o_r will  be unified with _T_e_r_m  and
  3752.     arity will be unified with the integer 0 (zero).
  3753.  
  3754.  
  3755. aarrgg((_?_A_r_g_, _?_T_e_r_m_, _?_V_a_l_u_e))
  3756.     _T_e_r_m  should be instantiated  to a term,  _A_r_g to an integer  between
  3757.     1  and  the  arity of  _T_e_r_m.    _V_a_l_u_e  is  unified with  the  _A_r_g-th
  3758.     argument  of _T_e_r_m.   _A_r_g may also  be unbound.   In this case  _V_a_l_u_e
  3759.     will  be unified  with the  successive arguments of  the term.    On
  3760.     successful  unification, _A_r_g  is unified  with the argument  number.
  3761.     Backtracking  yields alternative  solutions.    The predicate  aarrgg//33
  3762.     fails  silently if  _A_r_g= 0 or _A_r_g > _a_r_i_t_y and raises  the exception
  3763.     domain_error(not_less_then_zero, Arg)if _A_r_g <0.
  3764.  
  3765.  
  3766. sseettaarrgg((_+_A_r_g_, _+_T_e_r_m_, _+_V_a_l_u_e))
  3767.     Extra-logical  predicate.     Assigns the  _A_r_g-th  argument  of  the
  3768.     compound  term _T_e_r_m with the given _V_a_l_u_e.  The assignment  is undone
  3769.     if  backtracking brings the  state back into  a position before  the
  3770.     sseettaarrgg//33 call.
  3771.  
  3772.     This  predicate may  be used  for destructive  assignment to  terms,
  3773.     using them as and extra-logical storage bin.
  3774.  
  3775.  
  3776. _?_T_e_r_m =.. _?_L_i_s_t
  3777.     _L_i_s_t  is a list which head is the functor of _T_e_r_m and  the remaining
  3778.     arguments are the arguments  of the term.  Each of the arguments may
  3779.     be  a variable,  but not  both.   This predicate  is called  `Univ'.
  3780.     Examples:
  3781.  
  3782.     ?- foo(hello, X) =.. List.
  3783.  
  3784.     List = [foo, hello, X]
  3785.  
  3786.     ?- Term =.. [baz, foo(1)]
  3787.  
  3788.     Term = baz(foo(1))
  3789.  
  3790.  
  3791. nnuummbbeerrvvaarrss((_+_T_e_r_m_, _+_F_u_n_c_t_o_r_, _+_S_t_a_r_t_, _-_E_n_d))
  3792.     Unify  the free variables of _T_e_r_m  with a term constructed from  the
  3793.     atom  _F_u_n_c_t_o_r with one argument.  The argument is the number  of the
  3794.     variable.    Counting starts  at _S_t_a_r_t.    _E_n_d is  unified with  the
  3795.     number that should be given to the next variable.  Example:
  3796.  
  3797.     ?- numbervars(foo(A, B, A), this_is_a_variable, 0, End).
  3798.  
  3799.     A = this_is_a_variable(0)
  3800.     B = this_is_a_variable(1)
  3801.     End = 2
  3802.  
  3803.     In Edinburgh Prolog the  second argument is missing.  It is fixed to
  3804.     be $VAR.
  3805.  
  3806.  
  3807. ffrreeee__vvaarriiaabblleess((_+_T_e_r_m_, _-_L_i_s_t))
  3808.     Unify  _L_i_s_t with  a list of  variables, each  sharing with a  unique
  3809.     variable of _T_e_r_m.  For example:
  3810.  
  3811.     ?- free_variables(a(X, b(Y, X), Z), L).
  3812.  
  3813.     L = [G367, G366, G371]
  3814.     X = G367
  3815.     Y = G366
  3816.     Z = G371
  3817.  
  3818.  
  3819. ccooppyy__tteerrmm((_+_I_n_, _-_O_u_t))
  3820.     Make a copy of  term _I_n and unify the result with _O_u_t.  Ground parts
  3821.     of  _I_n are  shared by  _O_u_t.   Provided _I_n  and _O_u_t  have no  sharing
  3822.     variables  before  this call  they will  have  no sharing  variables
  3823.     afterwards.  ccooppyy__tteerrmm//22 is semantically equivalent to:
  3824.  
  3825.     copy_term(In, Out) :-
  3826.             recorda(copy_key, In, Ref),
  3827.             recorded(copy_key, Out, Ref),
  3828.             erase(Ref).
  3829.  
  3830.  
  3831. 33..1199 AAnnaallyyssiinngg aanndd CCoonnssttrruuccttiinngg AAttoommss
  3832.  
  3833. These predicates  convert between  Prolog constants and  lists of  ASCII
  3834. values.   The predicates  aattoomm__cchhaarrss//22, nnuummbbeerr__cchhaarrss//22 and nnaammee//22  behave
  3835. the same  when converting  from a constant  to a  list of ASCII  values.
  3836. When  converting the  other way  around,  aattoomm__cchhaarrss//22 will generate  an
  3837. atom, number_chars will generate a  number or fail and nnaammee//22 will return
  3838. a number if possible and an atom otherwise.
  3839.  
  3840.  
  3841. aattoomm__cchhaarrss((_?_A_t_o_m_, _?_S_t_r_i_n_g))
  3842.     Convert  between an atom  and a list  of ASCII values.   If _A_t_o_m  is
  3843.     instantiated, if will  be translated into a list of ASCII values and
  3844.     the  result is unified with _S_t_r_i_n_g.   If _A_t_o_m is unbound  and _S_t_r_i_n_g
  3845.     is  a list of  ASCII values, it  will _A_t_o_m will  be unified with  an
  3846.     atom constructed from this list.
  3847.  
  3848.  
  3849. aattoomm__cchhaarr((_?_A_t_o_m_, _?_A_S_C_I_I))
  3850.     Convert between character and ASCII value for a single character.
  3851.  
  3852.  
  3853. nnuummbbeerr__cchhaarrss((_?_N_u_m_b_e_r_, _?_S_t_r_i_n_g))
  3854.     Similar  to  aattoomm__cchhaarrss//22,  but  converts between  a number  and  its
  3855.     representation as a list  of ASCII values.  Fails silently if _N_u_m_b_e_r
  3856.     is unbound and _S_t_r_i_n_g does not describe a number.
  3857.  
  3858.  
  3859. nnaammee((_?_A_t_o_m_O_r_I_n_t_, _?_S_t_r_i_n_g))
  3860.     _S_t_r_i_n_g  is a  list of ASCII  values describing  _A_t_o_m.   Each of  the
  3861.     arguments may be a  variable, but not both.  When _S_t_r_i_n_g is bound to
  3862.     an  ASCII value list  describing an integer  and _A_t_o_m is a  variable
  3863.     _A_t_o_m  will be  unified with  the integer value  described by  _S_t_r_i_n_g
  3864.     (e.g.  `name(N, "300"), 400 is N + 100' succeeds).
  3865.  
  3866.  
  3867. iinntt__ttoo__aattoomm((_+_I_n_t_, _+_B_a_s_e_, _-_A_t_o_m))
  3868.     Convert  _I_n_t to an  ascii representation using  base _B_a_s_e and  unify
  3869.     the  result with _A_t_o_m.   If _B_a_s_e 6=10 the  base will be prepended  to
  3870.     _A_t_o_m.    _B_a_s_e= 0 will try  to interpret  _I_n_t as an  ASCII value and
  3871.     return  0'<_c>.   Otherwise  2 _B_a_s_e 36.    Some examples are  given
  3872.     below.
  3873.  
  3874.                    int_to_atom(45, 2, A)  -!   A= 20101101
  3875.                    int_to_atom(97, 0, A)  -!   A= 00a
  3876.                    int_to_atom(56, 10, A) -!   A= 56
  3877.  
  3878.  
  3879. iinntt__ttoo__aattoomm((_+_I_n_t_, _-_A_t_o_m))
  3880.     Equivalent to int_to_atom(Int, 10, Atom).
  3881.  
  3882.  
  3883. tteerrmm__ttoo__aattoomm((_?_T_e_r_m_, _?_A_t_o_m))
  3884.     Succeeds  if _A_t_o_m describes  a term  that unifies with  _T_e_r_m.   When
  3885.     _A_t_o_m  is instantiated _A_t_o_m is converted and then unified  with _T_e_r_m.
  3886.     Otherwise _T_e_r_m is ``written'' on _A_t_o_m using wwrriittee//11.
  3887.  
  3888.  
  3889. aattoomm__ttoo__tteerrmm((_+_A_t_o_m_, _-_T_e_r_m_, _-_B_i_n_d_i_n_g_s))
  3890.     Use  _A_t_o_m as input to  rreeaadd__vvaarriiaabblleess//22and  return the read term  in
  3891.     _T_e_r_m  and the variable bindings in _B_i_n_d_i_n_g_s.  _B_i_n_d_i_n_g_s is a  list of
  3892.     _N_a_m_e = _V_a_r couples,  thus providing  access to the  actual variable
  3893.     names.  See also rreeaadd__vvaarriiaabblleess//22.
  3894.  
  3895.  
  3896. ccoonnccaatt((_?_A_t_o_m_1_, _?_A_t_o_m_2_, _?_A_t_o_m_3))
  3897.     _A_t_o_m_3  forms the concatenation of _A_t_o_m_1 and _A_t_o_m_2.  At least  two of
  3898.     the  arguments must be instantiated  to atoms, integers or  floating
  3899.     point numbers.
  3900.  
  3901.  
  3902. ccoonnccaatt__aattoomm((_+_L_i_s_t_, _-_A_t_o_m))
  3903.     _L_i_s_t  is  a  list of  atoms,  integers  or floating  point  numbers.
  3904.     Succeeds  if _A_t_o_m can be  unified with the concatenated elements  of
  3905.     _L_i_s_t.  If  _L_i_s_t has exactly 2 elements it is equivalent to ccoonnccaatt//33,
  3906.     allowing for variables in the list.
  3907.  
  3908.  
  3909. ccoonnccaatt__aattoomm((_+_L_i_s_t_, _+_S_e_p_a_r_a_t_o_r_, _-_A_t_o_m))
  3910.     Creates  an  atom  just like  ccoonnccaatt__aattoomm//22,  but  inserts  _S_e_p_a_r_a_t_o_r
  3911.     between each pair of atoms.  For example:
  3912.  
  3913.     ?- concat_atom([gnu, gnat], ', ', A).
  3914.  
  3915.     A = 'gnu, gnat'
  3916.  
  3917.  
  3918. aattoomm__lleennggtthh((_+_A_t_o_m_, _-_L_e_n_g_t_h))
  3919.     Succeeds  if  _A_t_o_m is  an atom  of  _L_e_n_g_t_h characters  long.    This
  3920.     predicate also works  for integers and floats, expressing the number
  3921.     of characters output when given to wwrriittee//11.
  3922.  
  3923.  
  3924. aattoomm__pprreeffiixx((_+_A_t_o_m_, _+_P_r_e_f_i_x))
  3925.     Succeeds  if  _A_t_o_m starts  with the  characters from  _P_r_e_f_i_x.    Its
  3926.     behaviour  is equivalent  to ?- concat(Prefix, _, Atom), but  avoids
  3927.     the construction of an atom for the `remainder'.
  3928.  
  3929.  
  3930. 33..2200 RReepprreesseennttiinngg TTeexxtt iinn SSttrriinnggss
  3931.  
  3932. SWI-Prolog  supports the  data type  _s_t_r_i_n_g.   Strings  are  a time  and
  3933. space efficient  mechanism to handle  text in Prolog.   Atoms are  under
  3934. some circumstances  not suitable because garbage  collection on them  is
  3935. next  to impossible  (Although  it is  possible:   BIM_prolog  does  it).
  3936. Representing text as a  list of ASCII values is, from the  logical point
  3937. of view, the cleanest solution.  It however has two  drawbacks:  1) they
  3938. cannot be  distinguished from a  list of (small)  integers; and 2)  they
  3939. consume (in SWI-Prolog) 12 bytes for each character stored.
  3940.  
  3941. Within strings  each character only  requires 1 byte  storage.   Strings
  3942. live  on  the global  stack  and  their  storage is  thus  reclaimed  on
  3943. backtracking.  Garbage collection can easily deal with strings.
  3944.  
  3945. The ISO standard proposes " ..." is transformed into a  string object by
  3946. rreeaadd//11 and derivatives.   This poses  problems as in the old  convention
  3947. " ..." is transformed into a list of ascii characters.   For this reason
  3948. the style check option `string' is available (see ssttyyllee__cchheecckk//11).
  3949.  
  3950. The  set  of  predicates  associated  with  strings  is  incomplete  and
  3951. tentative.  Names and definitions might change in  the future to confirm
  3952. to the emerging standard.
  3953.  
  3954.  
  3955. ssttrriinngg__ttoo__aattoomm((_?_S_t_r_i_n_g_, _?_A_t_o_m))
  3956.     Logical  conversion between  a string  and an atom.    At least  one
  3957.     of  the two arguments  must be instantiated.   _A_t_o_m  can also be  an
  3958.     integer or floating point number.
  3959.  
  3960.  
  3961. ssttrriinngg__ttoo__lliisstt((_?_S_t_r_i_n_g_, _?_L_i_s_t))
  3962.     Logical conversion between  a string and a list of ASCII characters.
  3963.     At least one of the two arguments must be instantiated.
  3964.  
  3965.  
  3966. ssttrriinngg__lleennggtthh((_+_S_t_r_i_n_g_, _-_L_e_n_g_t_h))
  3967.     Unify  _L_e_n_g_t_h  with  the number  of  characters  in _S_t_r_i_n_g.     This
  3968.     predicate  is  functionally  equivalent  to  aattoomm__lleennggtthh//22 and  also
  3969.     accepts atoms, integers and floats as its first argument.
  3970.  
  3971.  
  3972. ssttrriinngg__ccoonnccaatt((_?_S_t_r_i_n_g_1_, _?_S_t_r_i_n_g_2_, _?_S_t_r_i_n_g_3))
  3973.     Similar  to ccoonnccaatt//33, but the unbound argument will be  unified with
  3974.     a  string  object rather  than  an atom.    Also,  if  both  _S_t_r_i_n_g_1
  3975.     and  _S_t_r_i_n_g_2 are  unbound and _S_t_r_i_n_g_3  is bound to  text, it  breaks
  3976.     _S_t_r_i_n_g_3,  unifying the start with  _S_t_r_i_n_g_1 and the end with  _S_t_r_i_n_g_2
  3977.     as  append does  with lists.   Note  that this  is not  particularly
  3978.     fast  on long  strings as  for each redo  the system  has to  create
  3979.     two  entirely new strings, while the list equivalent only  creates a
  3980.     single new list-cell and moves some pointers around.
  3981.  
  3982.  
  3983. ssuubbssttrriinngg((_+_S_t_r_i_n_g_, _+_S_t_a_r_t_, _+_L_e_n_g_t_h_, _-_S_u_b))
  3984.     Create  a substring  of  _S_t_r_i_n_g that  starts at  character _S_t_a_r_t  (1
  3985.     base) and has _L_e_n_g_t_h characters.  Unify this substring with _S_u_b.
  3986.  
  3987.  
  3988. 33..2211 OOppeerraattoorrss
  3989.  
  3990.  
  3991. oopp((_+_P_r_e_c_e_d_e_n_c_e_, _+_T_y_p_e_, _+_N_a_m_e))
  3992.     Declare  _N_a_m_e  to  be  an  operator of  type  _T_y_p_e  with  precedence
  3993.     _P_r_e_c_e_d_e_n_c_e.    _N_a_m_e  can also  be a  list of  names,  in which  case
  3994.     all  elements of the  list are declared  to be identical  operators.
  3995.     _P_r_e_c_e_d_e_n_c_e  is an integer between 0 and 1200.  Precedence  0 removes
  3996.     the  declaration.   _T_y_p_e is one  of:   xf, yf, xfx,  xfy, yfx,  yfy,
  3997.     fy  or fx.   The `f'  indicates the position  of the functor,  while
  3998.     x  and y  indicate the position  of the  arguments.   `y' should  be
  3999.     interpreted  as ``on this position  a term with precedence lower  or
  4000.     equal to the precedence  of the functor should occur''.  For `x' the
  4001.     precedence  of the argument must be strictly lower.   The precedence
  4002.     of  a term is  0, unless  its principal functor  is an operator,  in
  4003.     which  case the precedence  is the precedence of  this operator.   A
  4004.     term enclosed in brackets (...) has precedence 0.
  4005.  
  4006.     The  predefined operators are  shown in  table 3.1.   Note that  all
  4007.     operators can be redefined by the user.
  4008.      ______________________________________________________________
  4009.      | 1200 |xfx  |-->, :-                                        |
  4010.      | 1200 | fx  |:-, ?-                                         |
  4011.      | 1150 | fx  |dynamic, multifile, module_transparent, discon-|
  4012.      |      |     |tiguous, volatile, initialization              |
  4013.      | 1100 |xfy  |;, |                                           |
  4014.  
  4015.      | 1050 |xfy  |->                                             |
  4016.      | 1000 |xfy  |,                                              |
  4017.      |  954 |xfy  |\                                              |
  4018.      |  900 | fy  |\+, not                                        |
  4019.      |  900 | fx  |~                                              |
  4020.      |  700 |xfx  |<, =, =.., =@=,  =:=, =<, ==, =\=,  >, >=, @<, |
  4021.      |      |     |@=<, @>, @>=, \=, \==, is                      |
  4022.      |  600 |xfy  |:                                              |
  4023.  
  4024.      |  500 | yfx |+, -, /\, \/, xor                              |
  4025.      |  500 | fx  |+, -, ?, \                                     |
  4026.      |  400 | yfx |*, /, //, <<, >>, mod, rem                     |
  4027.      |  200 |xfx  |**                                             |
  4028.      |__200_|xfy__|^______________________________________________|_
  4029.  
  4030.                       Table 3.1:  System operators
  4031.  
  4032.  
  4033. ccuurrrreenntt__oopp((_?_P_r_e_c_e_d_e_n_c_e_, _?_T_y_p_e_, _?_N_a_m_e))
  4034.     Succeeds when _N_a_m_e  is currently defined as an operator of type _T_y_p_e
  4035.     with precedence _P_r_e_c_e_d_e_n_c_e.  See also oopp//33.
  4036.  
  4037.  
  4038. 33..2222 AArriitthhmmeettiicc
  4039.  
  4040. Arithmetic can be  divided into some special purpose integer  predicates
  4041. and  a series  of  general predicates  for  floating point  and  integer
  4042. arithmetic as  appropriate.  The  integer predicates are as  ``logical''
  4043. as possible.  Their usage is recommended  whenever applicable, resulting
  4044. in faster and more ``logical'' programs.
  4045.  
  4046. The  general arithmetic  predicates  are  optionally compiled  now  (see
  4047. pplleeaassee//33 and the -O  command line option).  Compiled  arithmetic reduces
  4048. global  stack requirements  and  improves  performance.    Unfortunately
  4049. compiled arithmetic cannot be traced, which is why it is optional.
  4050.  
  4051. The  general  arithmetic   predicates  all  handle  _e_x_p_r_e_s_s_i_o_n_s.      An
  4052. expression is either a simple number or a _f_u_n_c_t_i_o_n.   The arguments of a
  4053. function are expressions.  The functions are described in section 3.23.
  4054.  
  4055.  
  4056. bbeettwweeeenn((_+_L_o_w_, _+_H_i_g_h_, _?_V_a_l_u_e))
  4057.     _L_o_w  and _H_i_g_h  are integers, _H_i_g_h  _L_o_w.   If _V_a_l_u_e  is an integer,
  4058.     _L_o_w  _V_a_l_u_e _H_i_g_h.   When _V_a_l_u_e  is a  variable it  is successively
  4059.     bound to all integers between _L_o_w and _H_i_g_h.
  4060.  
  4061.  
  4062. ssuucccc((_?_I_n_t_1_, _?_I_n_t_2))
  4063.     Succeeds  if _I_n_t_2= _I_n_t_1+ 1.  At least one of  the arguments must be
  4064.     instantiated to an integer.
  4065.  
  4066.  
  4067. pplluuss((_?_I_n_t_1_, _?_I_n_t_2_, _?_I_n_t_3))
  4068.     Succeeds if  _I_n_t_3= _I_n_t_1+_I_n_t_2.   At least two of the three arguments
  4069.     must be instantiated to integers.
  4070.  
  4071.  
  4072. _+_E_x_p_r_1 > _+_E_x_p_r_2
  4073.     Succeeds  when expression  _E_x_p_r_1 evaluates to  a larger number  than
  4074.     _E_x_p_r_2.
  4075.  
  4076.  
  4077. _+_E_x_p_r_1 < _+_E_x_p_r_2
  4078.     Succeeds  when expression _E_x_p_r_1 evaluates  to a smaller number  than
  4079.     _E_x_p_r_2.
  4080.  
  4081.  
  4082. _+_E_x_p_r_1 =< _+_E_x_p_r_2
  4083.     Succeeds  when  expression _E_x_p_r_1  evaluates to  a  smaller or  equal
  4084.     number to _E_x_p_r_2.
  4085.  
  4086.  
  4087. _+_E_x_p_r_1 >= _+_E_x_p_r_2
  4088.     Succeeds  when  expression  _E_x_p_r_1 evaluates  to  a larger  or  equal
  4089.     number to _E_x_p_r_2.
  4090.  
  4091.  
  4092. _+_E_x_p_r_1 =\= _+_E_x_p_r_2
  4093.     Succeeds  when expression _E_x_p_r_1 evaluates  to a number non-equal  to
  4094.     _E_x_p_r_2.
  4095.  
  4096.  
  4097. _+_E_x_p_r_1 =:= _+_E_x_p_r_2
  4098.     Succeeds  when  expression  _E_x_p_r_1 evaluates  to  a number  equal  to
  4099.     _E_x_p_r_2.
  4100.  
  4101.  
  4102. _-_N_u_m_b_e_r iiss _+_E_x_p_r
  4103.     Succeeds  when  _N_u_m_b_e_r   has  successfully  been  unified  with  the
  4104.     number  _E_x_p_r  evaluates to.    If  _E_x_p_r evaluates  to a  float  that
  4105.     can  be represented  using  an integer  (i.e. the  value is  integer
  4106.     and  within the  range  that can  be described  by Prolog's  integer
  4107.     representation),  _E_x_p_r is unified with the integer value.
  4108.  
  4109.     Note  that normally, iiss//22  will be used  with unbound left  operand.
  4110.     If equality is to be tested, =:=/2 should be used.  For example:
  4111.  
  4112.          ?- 1.0 is sin(pi/2).         Fails!.   sin(pi/2) evaluates
  4113.                                       to   1.0,   but   iiss//22   will
  4114.                                       represent this as the integer
  4115.                                       1,  after  which  unify  will
  4116.                                       fail.
  4117.  
  4118.          ?- 1.0 is float(sin(pi/2)).  Succeeds,   as   the  ffllooaatt//11
  4119.                                       function forces the result to
  4120.                                       be float.
  4121.          ?- 1.0 =:= sin(pi/2).        Succeeds as expected.
  4122.  
  4123.  
  4124. 33..2233 AArriitthhmmeettiicc FFuunnccttiioonnss
  4125.  
  4126. Arithmetic functions  are terms  which are evaluated  by the  arithmetic
  4127. predicates described  above.   SWI-Prolog tries  to hide the  difference
  4128. between  integer  arithmetic  and floating  point  arithmetic  from  the
  4129. Prolog  user.   Arithmetic  is done  as integer  arithmetic  as long  as
  4130. possible  and converted  to floating  point arithmetic  whenever one  of
  4131. the arguments  or the combination of  them requires it.   If a  function
  4132. returns  a floating  point  value which  is  whole it  is  automatically
  4133. transformed into  an integer.   There  are three  types of arguments  to
  4134. functions:
  4135.  
  4136.         _E_x_p_r     Arbitrary   expression,   returning  either   a
  4137.                  floating point value or an integer.
  4138.         _I_n_t_E_x_p_r  Arbitrary expression that should  evaluate into
  4139.                  an integer.
  4140.         _I_n_t      An integer.
  4141.  
  4142. In  case integer  addition, subtraction  and  multiplication would  lead
  4143. to  an integer  overflow  the operands  are automatically  converted  to
  4144. floating point  numbers.   The floating  point functions (ssiinn//11,  eexxpp//11,
  4145. etc.)     form  a  direct  interface  to  the  corresponding  C  library
  4146. functions used  to compile SWI-Prolog.   Please refer  to the C  library
  4147. documentation for details on precision, error handling, etc.
  4148.  
  4149.  
  4150. - _+_E_x_p_r
  4151.     _R_e_s_u_l_t =-_E_x_p_r
  4152.  
  4153.  
  4154. _+_E_x_p_r_1 + _+_E_x_p_r_2
  4155.     _R_e_s_u_l_t =_E_x_p_r_1 +_E_x_p_r_2
  4156.  
  4157.  
  4158. _+_E_x_p_r_1 - _+_E_x_p_r_2
  4159.     _R_e_s_u_l_t =_E_x_p_r_1 -_E_x_p_r_2
  4160.  
  4161.  
  4162. _+_E_x_p_r_1 * _+_E_x_p_r_2
  4163.     _R_e_s_u_l_t =_E_x_p_r_1_*Expr2
  4164.  
  4165.  
  4166. _+_E_x_p_r_1 / _+_E_x_p_r_2
  4167.     _R_e_s_u_l_t =_E_x_p_r_1=_E_x_p_r_2
  4168.  
  4169.  
  4170. _+_I_n_t_E_x_p_r_1 mmoodd _+_I_n_t_E_x_p_r_2
  4171.     _R_e_s_u_l_t =_E_x_p_r_1mod_E_x_p_r_2 (remainder of division).
  4172.  
  4173.  
  4174. _+_I_n_t_E_x_p_r_1 rreemm _+_I_n_t_E_x_p_r_2
  4175.     _R_e_s_u_l_t =_E_x_p_r_1rem_E_x_p_r_2 (remainder of division).
  4176.  
  4177.  
  4178. _+_I_n_t_E_x_p_r_1 // _+_I_n_t_E_x_p_r_2
  4179.     _R_e_s_u_l_t =_E_x_p_r_1 _E_x_p_r_2 (integer division).
  4180.  
  4181.  
  4182. aabbss((_+_E_x_p_r))
  4183.     Evaluate _E_x_p_r and return the absolute value of it.
  4184.  
  4185.  
  4186. ssiiggnn((_+_E_x_p_r))
  4187.     Evaluate to -1 if _E_x_p_r <0, 1 if _E_x_p_r >0 and 0 if _E_x_p_r =0.
  4188.  
  4189.  
  4190. mmaaxx((_+_E_x_p_r_1_, _+_E_x_p_r_2))
  4191.     Evaluates to the largest of both _E_x_p_r_1 and _E_x_p_r_2.
  4192.  
  4193.  
  4194. mmiinn((_+_E_x_p_r_1_, _+_E_x_p_r_2))
  4195.     Evaluates to the smallest of both _E_x_p_r_1 and _E_x_p_r_2.
  4196.  
  4197.  
  4198. .((_+_I_n_t_, _[_]))
  4199.     A  list of  one  element evaluates  to the  element.   This  implies
  4200.     "a"  evaluates to  the ASCII value  of the  letter `a' (97).    This
  4201.     option  is available for  compatibility only.   It will not work  if
  4202.     `style_check(+string)'  is active as  "a" will  then be  transformed
  4203.     into  a string object.    The recommended way  to specify the  ASCII
  4204.     value of the letter `a' is 0'a.
  4205.  
  4206.  
  4207. rraannddoomm((_+_I_n_t))
  4208.     Evaluates  to a  random integer _i  for which  0 i< _I_n_t.   The seed
  4209.     of  this random  generator is  determined by the  system clock  when
  4210.     SWI-Prolog was started.
  4211.  
  4212.  
  4213. rroouunndd((_+_E_x_p_r))
  4214.     Evaluates _E_x_p_r and rounds the result to the nearest integer.
  4215.  
  4216.  
  4217. iinntteeggeerr((_+_E_x_p_r))
  4218.     Same as rroouunndd//11 (backward compatibility).
  4219.  
  4220.  
  4221. ffllooaatt((_+_E_x_p_r))
  4222.     Translate  the result to a floating point number.   Normally, Prolog
  4223.     will  use integers  whenever possible.    When used  around the  2nd
  4224.     argument  of iiss//22, the result will  be returned as a floating  point
  4225.     number.  In other contexts, the operation has no effect.
  4226.  
  4227.  
  4228. ffllooaatt__ffrraaccttiioonnaall__ppaarrtt((_+_E_x_p_r))
  4229.     Fractional  part of a  floating-point number.   Negative if _E_x_p_r  is
  4230.     negative, 0 if _E_x_p_r is integer.
  4231.  
  4232.  
  4233. ffllooaatt__iinntteeggeerr__ppaarrtt((_+_E_x_p_r))
  4234.     Integer  part  of  floating-point  number.    Negative  if  _E_x_p_r  is
  4235.     negative, _E_x_p_r if _E_x_p_r is integer.
  4236.  
  4237.  
  4238. ttrruunnccaattee((_+_E_x_p_r))
  4239.     Truncate _E_x_p_r to an integer.  Same as ffllooaatt__iinntteeggeerr__ppaarrtt//11.
  4240.  
  4241.  
  4242. fflloooorr((_+_E_x_p_r))
  4243.     Evaluates  _E_x_p_r and returns the largest integer smaller or  equal to
  4244.     the result of the evaluation.
  4245.  
  4246.  
  4247. cceeiilliinngg((_+_E_x_p_r))
  4248.     Evaluates  _E_x_p_r and returns the smallest integer larger or  equal to
  4249.     the result of the evaluation.
  4250.  
  4251.  
  4252. cceeiill((_+_E_x_p_r))
  4253.     Same as cceeiilliinngg//11 (backward compatibility).
  4254.  
  4255.  
  4256. _+_I_n_t_E_x_p_r >> _+_I_n_t_E_x_p_r
  4257.     Bitwise shift _I_n_t_E_x_p_r_1 by _I_n_t_E_x_p_r_2 bits to the right.
  4258.  
  4259.  
  4260. _+_I_n_t_E_x_p_r << _+_I_n_t_E_x_p_r
  4261.     Bitwise shift _I_n_t_E_x_p_r_1 by _I_n_t_E_x_p_r_2 bits to the left.
  4262.  
  4263.  
  4264. _+_I_n_t_E_x_p_r \/ _+_I_n_t_E_x_p_r
  4265.     Bitwise `or' _I_n_t_E_x_p_r_1 and _I_n_t_E_x_p_r_2.
  4266.  
  4267.  
  4268. _+_I_n_t_E_x_p_r /\ _+_I_n_t_E_x_p_r
  4269.     Bitwise `and' _I_n_t_E_x_p_r_1 and _I_n_t_E_x_p_r_2.
  4270.  
  4271.  
  4272. _+_I_n_t_E_x_p_r xxoorr _+_I_n_t_E_x_p_r
  4273.     Bitwise `exclusive or' _I_n_t_E_x_p_r_1 and _I_n_t_E_x_p_r_2.
  4274.  
  4275.  
  4276. \ _+_I_n_t_E_x_p_r
  4277.     Bitwise negation.
  4278.  
  4279.  
  4280. ssqqrrtt((_+_E_x_p_r))
  4281.     _R_e_s_u_l_t =square root of _E_x_p_r
  4282.  
  4283.  
  4284. ssiinn((_+_E_x_p_r))
  4285.     _R_e_s_u_l_t =sine of _E_x_p_r.  _E_x_p_r is the angle in radians.
  4286.  
  4287.  
  4288. ccooss((_+_E_x_p_r))
  4289.     _R_e_s_u_l_t =cosine of _E_x_p_r.  _E_x_p_r is the angle in radians.
  4290.  
  4291.  
  4292. ttaann((_+_E_x_p_r))
  4293.     _R_e_s_u_l_t =tangus of _E_x_p_r.  _E_x_p_r is the angle in radians.
  4294.  
  4295.  
  4296. aassiinn((_+_E_x_p_r))
  4297.     _R_e_s_u_l_t =inverse sine of _E_x_p_r.  _R_e_s_u_l_t is the angle in radians.
  4298.  
  4299.  
  4300. aaccooss((_+_E_x_p_r))
  4301.     _R_e_s_u_l_t =inverse cosine of _E_x_p_r.  _R_e_s_u_l_t is the angle in radians.
  4302.  
  4303.  
  4304. aattaann((_+_E_x_p_r))
  4305.     _R_e_s_u_l_t =inverse tangus of _E_x_p_r.  _R_e_s_u_l_t is the angle in radians.
  4306.  
  4307.  
  4308. aattaann((_+_Y_E_x_p_r_, _+_X_E_x_p_r))
  4309.     _R_e_s_u_l_t = inverse tangus of _Y_E_x_p_r / _X_E_x_p_r.   _R_e_s_u_l_t is  the angle in
  4310.     radians.    The return  value is  in the  range [-pi:::pi].   Used  to
  4311.     convert between rectangular and polar coordinate system.
  4312.  
  4313.  
  4314. lloogg((_+_E_x_p_r))
  4315.     _R_e_s_u_l_t =natural logarithm of _E_x_p_r
  4316.  
  4317.  
  4318. lloogg1100((_+_E_x_p_r))
  4319.     _R_e_s_u_l_t =10 base logarithm of _E_x_p_r
  4320.  
  4321.  
  4322. eexxpp((_+_E_x_p_r))
  4323.     _R_e_s_u_l_t =e to the power _E_x_p_r
  4324.  
  4325.  
  4326. _+_E_x_p_r_1 ** _+_E_x_p_r_2
  4327.     _R_e_s_u_l_t =_E_x_p_r_1 to the power _E_x_p_r_2
  4328.  
  4329.  
  4330. _+_E_x_p_r_1 ^ _+_E_x_p_r_2
  4331.     Same as **/2.  (backward compatibility).
  4332.  
  4333.  
  4334. ppii
  4335.     Evaluates to the mathematical constant pi (3.141593).
  4336.  
  4337.  
  4338. ee
  4339.     Evaluates to the mathematical constant e (2.718282).
  4340.  
  4341.  
  4342. ccppuuttiimmee
  4343.     Evaluates  to a floating  point number expressing  the cpu time  (in
  4344.     seconds)  used by Prolog  up till  now.   See also ssttaattiissttiiccss//22  and
  4345.     ttiimmee//11.
  4346.  
  4347.  
  4348. 33..2244 AAddddiinngg AArriitthhmmeettiicc FFuunnccttiioonnss
  4349.  
  4350. Prolog predicates  can be given  the role of arithmetic  function.   The
  4351. last  argument is  used  to  return the  result,  the  arguments  before
  4352. the last  are the  inputs.   Arithmetic  functions are  added using  the
  4353. predicate aarriitthhmmeettiicc__ffuunnccttiioonn//11, which takes  the head as its  argument.
  4354. Arithmetic  functions  are module  sensitive,  that  is  they  are  only
  4355. visible from the module  in which the function is defined  and declared.
  4356. Global  arithmetic  functions  should be  defined  and  registered  from
  4357. module user.   Global definitions can  be overruled locally in  modules.
  4358. The builtin functions described above can be redefined as well.
  4359.  
  4360.  
  4361. aarriitthhmmeettiicc__ffuunnccttiioonn((_+_H_e_a_d))
  4362.     Register  a Prolog  predicate as an  arithmetic function (see  iiss//22,
  4363.     >//22 , etc.).   The Prolog  predicate should  have one more  argument
  4364.     than  specified by _H_e_a_d, which it either a term _N_a_m_e_/_A_r_i_t_y,  an atom
  4365.     or  a complex term.   This last argument  is an unbound variable  at
  4366.     call  time and  should  be instantiated  to an  integer or  floating
  4367.     point  number.    The other  arguments  are the  parameters.    This
  4368.     predicate  is  module  sensitive  and will  declare  the  arithmetic
  4369.     function  only for the context  module, unless declared from  module
  4370.     user.  Example:
  4371.  
  4372.     1 ?- [user].
  4373.     :- arithmetic_function(mean/2).
  4374.  
  4375.     mean(A, B, C) :-
  4376.             C is (A+B)/2.
  4377.     user compiled, 0.07 sec, 440 bytes.
  4378.  
  4379.     Yes
  4380.     2 ?- A is mean(4, 5).
  4381.  
  4382.     A = 4.500000
  4383.  
  4384.  
  4385. ccuurrrreenntt__aarriitthhmmeettiicc__ffuunnccttiioonn((_?_H_e_a_d))
  4386.     Successively unifies all  arithmetic functions that are visible from
  4387.     the context module with _H_e_a_d.
  4388.  
  4389.  
  4390. 33..2255 LLiisstt MMaanniippuullaattiioonn
  4391.  
  4392.  
  4393. iiss__lliisstt((_+_T_e_r_m))
  4394.     Succeeds  if _T_e_r_m is  bound to the  empty list ([])  or a term  with
  4395.     functor `.' and arity 2.
  4396.  
  4397.  
  4398. pprrooppeerr__lliisstt((_+_T_e_r_m))
  4399.     Equivalent  to iiss__lliisstt//11, but  also requires the tail of the  list to
  4400.     be a list (recursively).  Examples:
  4401.  
  4402.     is_list([x|A])          % true
  4403.     proper_list([x|A])      % false
  4404.  
  4405.  
  4406. aappppeenndd((_?_L_i_s_t_1_, _?_L_i_s_t_2_, _?_L_i_s_t_3))
  4407.     Succeeds  when _L_i_s_t_3  unifies with  the concatenation  of _L_i_s_t_1  and
  4408.     _L_i_s_t_2.   The  predicate can be  used with any instantiation  pattern
  4409.     (even three variables).
  4410.  
  4411.  
  4412. mmeemmbbeerr((_?_E_l_e_m_, _?_L_i_s_t))
  4413.     Succeeds  when _E_l_e_m can be unified with one of the members  of _L_i_s_t.
  4414.     The predicate can be used with any instantiation pattern.
  4415.  
  4416.  
  4417. mmeemmbbeerrcchhkk((_?_E_l_e_m_, _+_L_i_s_t))
  4418.     Equivalent to mmeemmbbeerr//22, but leaves no choice point.
  4419.  
  4420.  
  4421. ddeelleettee((_+_L_i_s_t_1_, _?_E_l_e_m_, _?_L_i_s_t_2))
  4422.     Delete all members  of _L_i_s_t_1 that simultaneously unify with _E_l_e_m and
  4423.     unify the result with _L_i_s_t_2.
  4424.  
  4425.  
  4426. sseelleecctt((_?_L_i_s_t_1_, _?_E_l_e_m_, _?_L_i_s_t_2))
  4427.     Select  an  element of  _L_i_s_t_1 that  unifies  with _E_l_e_m.    _L_i_s_t_2  is
  4428.     unified  with  the  list remaining  from  _L_i_s_t_1 after  deleting  the
  4429.     selected  element.   Normally  used with  the instantiation  pattern
  4430.     _+_L_i_s_t_1_, _-_E_l_e_m_, _-_L_i_s_t_2,  but can also be used to insert an element in
  4431.     a list using _-_L_i_s_t_1_, _+_E_l_e_m_, _+_L_i_s_t_2.
  4432.  
  4433.  
  4434. nntthh00((_?_I_n_d_e_x_, _?_L_i_s_t_, _?_E_l_e_m))
  4435.     Succeeds  when  the  _I_n_d_e_x-th element  of  _L_i_s_t unifies  with  _E_l_e_m.
  4436.     Counting starts at 0.
  4437.  
  4438.  
  4439. nntthh11((_?_I_n_d_e_x_, _?_L_i_s_t_, _?_E_l_e_m))
  4440.     Succeeds  when  the  _I_n_d_e_x-th element  of  _L_i_s_t unifies  with  _E_l_e_m.
  4441.     Counting starts at 1.
  4442.  
  4443.  
  4444. llaasstt((_?_E_l_e_m_, _?_L_i_s_t))
  4445.     Succeeds if _E_l_e_m unifies with the last element of _L_i_s_t.
  4446.  
  4447.  
  4448. rreevveerrssee((_+_L_i_s_t_1_, _-_L_i_s_t_2))
  4449.     Reverse  the order  of the elements  in _L_i_s_t_1  and unify the  result
  4450.     with the elements of _L_i_s_t_2.
  4451.  
  4452.  
  4453. ffllaatttteenn((_+_L_i_s_t_1_, _-_L_i_s_t_2))
  4454.     Transform  _L_i_s_t_1, possibly holding lists  as elements into a  `flat'
  4455.     list by replacing  each list with its elements (recursively).  Unify
  4456.     the resulting flat list with _L_i_s_t_2.  Example:
  4457.  
  4458.     ?- flatten([a, [b, [c, d], e]], X).
  4459.  
  4460.     X = [a, b, c, d, e]
  4461.  
  4462.  
  4463. lleennggtthh((_?_L_i_s_t_, _?_I_n_t))
  4464.     Succeeds  if _I_n_t  represents the  number of elements  of list  _L_i_s_t.
  4465.     Can be used to create a list holding only variables.
  4466.  
  4467.  
  4468. mmeerrggee((_+_L_i_s_t_1_, _+_L_i_s_t_2_, _-_L_i_s_t_3))
  4469.     _L_i_s_t_1  and _L_i_s_t_2 are  lists, sorted to  the standard order of  terms
  4470.     (see  section 3.5).    _L_i_s_t_3 will be  unified with  an ordered  list
  4471.     holding  both the elements of _L_i_s_t_1  and _L_i_s_t_2.  Duplicates are  nnoott
  4472.     removed.
  4473.  
  4474.  
  4475. 33..2266 SSeett MMaanniippuullaattiioonn
  4476.  
  4477.  
  4478. iiss__sseett((_+_S_e_t))
  4479.     Succeeds  if  _S_e_t  is  a  proper  list  (see pprrooppeerr__lliisstt//11)  without
  4480.     duplicates.
  4481.  
  4482.  
  4483. lliisstt__ttoo__sseett((_+_L_i_s_t_, _-_S_e_t))
  4484.     Unifies  _S_e_t  with a  list  holding the  same  elements as  _L_i_s_t  in
  4485.     the  same order.   If  _l_i_s_t contains duplicates,  only the first  is
  4486.     retained.  See also ssoorrtt//22.  Example:
  4487.  
  4488.     ?- list_to_set([a,b,a], X)
  4489.  
  4490.     X = [a,b]
  4491.  
  4492.  
  4493. iinntteerrsseeccttiioonn((_+_S_e_t_1_, _+_S_e_t_2_, _-_S_e_t_3))
  4494.     Succeeds  if _S_e_t_3 unifies  with the intersection  of _S_e_t_1 and  _S_e_t_2.
  4495.     _S_e_t_1  and _S_e_t_2  are  lists without  duplicates.   They  need not  be
  4496.     ordered.
  4497.  
  4498.  
  4499. ssuubbttrraacctt((_+_S_e_t_, _+_D_e_l_e_t_e_, _-_R_e_s_u_l_t))
  4500.     Delete  all  elements  of set  `Delete'  from  `Set' and  unify  the
  4501.     resulting set with `Result'.
  4502.  
  4503.  
  4504. uunniioonn((_+_S_e_t_1_, _+_S_e_t_2_, _-_S_e_t_3))
  4505.     Succeeds if _S_e_t_3 unifies  with the union of _S_e_t_1 and _S_e_t_2.  _S_e_t_1 and
  4506.     _S_e_t_2 are lists without duplicates.  They need not be ordered.
  4507.  
  4508.  
  4509. ssuubbsseett((_+_S_u_b_s_e_t_, _+_S_e_t))
  4510.     Succeeds if all elements of _S_u_b_s_e_t are elements of _S_e_t as well.
  4511.  
  4512.  
  4513. mmeerrggee__sseett((_+_S_e_t_1_, _+_S_e_t_2_, _-_S_e_t_3))
  4514.     _S_e_t_1  and _S_e_t_2 are lists without duplicates, sorted to  the standard
  4515.     order  of terms.    _S_e_t_3  is unified  with an  ordered list  without
  4516.     duplicates holding the union of the elements of _S_e_t_1 and _S_e_t_2.
  4517.  
  4518.  
  4519. 33..2277 SSoorrttiinngg LLiissttss
  4520.  
  4521.  
  4522. ssoorrtt((_+_L_i_s_t_, _-_S_o_r_t_e_d))
  4523.     Succeeds  if _S_o_r_t_e_d can be unified with a list holding  the elements
  4524.     of  _L_i_s_t, sorted to the standard  order of terms (see section  3.5).
  4525.     Duplicates  are removed.  Implemented by translating the  input list
  4526.     into  a temporary  array,  calling the  C-library function  qqssoorrtt((3))
  4527.     using  PPLL__ccoommppaarree(()) for  comparing  the elements,  after  which  the
  4528.     result is translated into the result list.
  4529.  
  4530.  
  4531. mmssoorrtt((_+_L_i_s_t_, _-_S_o_r_t_e_d))
  4532.     Equivalent to ssoorrtt//22, but does not remove duplicates.
  4533.  
  4534.  
  4535. kkeeyyssoorrtt((_+_L_i_s_t_, _-_S_o_r_t_e_d))
  4536.     _L_i_s_t  is  a list  of  Key-Value pairs  (e.g.  terms of  the  functor
  4537.     `-'  with  arity  2).    kkeeyyssoorrtt//22  sorts  _L_i_s_t  like  mmssoorrtt//22,  but
  4538.     only  compares  the  keys.    Can  be  used  to sort  terms  not  on
  4539.     standard  order, but  on any criterion  that can  be expressed on  a
  4540.     multi-dimensional scale.   Sorting on more than one criterion can be
  4541.     done  using terms as keys,  putting the first criterion as  argument
  4542.     1,  the second as argument 2,  etc.  The order of  multiple elements
  4543.     that have the same _K_e_y is not changed.
  4544.  
  4545.  
  4546. pprreeddssoorrtt((_+_P_r_e_d_, _+_L_i_s_t_, _-_S_o_r_t_e_d))
  4547.     Sorts  similar to mmssoorrtt//22, but determines the order of two  terms by
  4548.     applying  _P_r_e_d to pairs  of elements from _L_i_s_t  (see aappppllyy//22).   The
  4549.     predicate  should succeed if the first element should be  before the
  4550.     second.
  4551.  
  4552.  
  4553. 33..2288 FFiinnddiinngg aallll SSoolluuttiioonnss ttoo aa GGooaall
  4554.  
  4555.  
  4556. ffiinnddaallll((_+_V_a_r_, _+_G_o_a_l_, _-_B_a_g))
  4557.     Creates  a  list  of the  instantiations  _V_a_r gets  successively  on
  4558.     backtracking  over _G_o_a_l and unifies the  result with _B_a_g.   Succeeds
  4559.     with  an  empty  list  if _G_o_a_l  has  no  solutions.    ffiinnddaallll//33  is
  4560.     equivalent  to  bbaaggooff//33  with  all free  variables  bound  with  the
  4561.     existence  operator (^), except that bbaaggooff//33 fails when goal  has no
  4562.     solutions.
  4563.  
  4564.  
  4565. bbaaggooff((_+_V_a_r_, _+_G_o_a_l_, _-_B_a_g))
  4566.     Unify  _B_a_g with the alternatives of _V_a_r, if _G_o_a_l has  free variables
  4567.     besides  the one  sharing  with _V_a_r  bagof will  backtrack over  the
  4568.     alternatives  of  these  free  variables,   unifying  _B_a_g  with  the
  4569.     corresponding  alternatives of _V_a_r.   The construct +Var^Goal  tells
  4570.     bagof  not to  bind  _V_a_r in  _G_o_a_l.   bbaaggooff//33  fails if  _G_o_a_l has  no
  4571.     solutions.
  4572.  
  4573.     The  example below  illustrates bbaaggooff//33  and the  ^ operator.    The
  4574.     variable bindings are printed together on one line to save paper.
  4575.  
  4576.     2 ?- listing(foo).
  4577.  
  4578.     foo(a, b, c).
  4579.     foo(a, b, d).
  4580.     foo(b, c, e).
  4581.     foo(b, c, f).
  4582.     foo(c, c, g).
  4583.  
  4584.     Yes
  4585.     3 ?- bagof(C, foo(A, B, C), Cs).
  4586.  
  4587.     A = a, B = b, C = G308, Cs = [c, d] ;
  4588.     A = b, B = c, C = G308, Cs = [e, f] ;
  4589.     A = c, B = c, C = G308, Cs = [g] ;
  4590.  
  4591.     No
  4592.     4 ?- bagof(C, A^foo(A, B, C), Cs).
  4593.  
  4594.     A = G324, B = b, C = G326, Cs = [c, d] ;
  4595.     A = G324, B = c, C = G326, Cs = [e, f, g] ;
  4596.  
  4597.     No
  4598.     5 ?-
  4599.  
  4600.  
  4601. sseettooff((_+_V_a_r_, _+_G_o_a_l_, _-_S_e_t))
  4602.     Equivalent  to bbaaggooff//33, but sorts the  result using ssoorrtt//22 to get  a
  4603.     sorted list of alternatives without duplicates.
  4604.  
  4605.  
  4606. 33..2299 IInnvvookkiinngg PPrreeddiiccaatteess oonn aallll MMeemmbbeerrss ooff aa LLiisstt
  4607.  
  4608. All the predicates  in this section call  a predicate on all members  of
  4609. a list  or until the predicate  called fails.   The predicate is  called
  4610. via aappppllyy//22, which implies  common arguments can be put in front  of the
  4611. arguments obtained from the list(s).  For example:
  4612.  
  4613. ?- maplist(plus(1), [0, 1, 2], X).
  4614.  
  4615. X = [1, 2, 3]
  4616.  
  4617. we will phrase this as ``_P_r_e_d_i_c_a_t_e is applied on ...''
  4618.  
  4619.  
  4620. cchheecckklliisstt((_+_P_r_e_d_, _+_L_i_s_t))
  4621.     _P_r_e_d  is applied successively on each element of _L_i_s_t until  the end
  4622.     of  the list  or _P_r_e_d fails.    In the latter  case the  cchheecckklliisstt//22
  4623.     fails.
  4624.  
  4625.  
  4626. mmaapplliisstt((_+_P_r_e_d_, _?_L_i_s_t_1_, _?_L_i_s_t_2))
  4627.     Apply  _P_r_e_d  on all  successive  pairs of  elements from  _L_i_s_t_1  and
  4628.     _L_i_s_t_2.    Fails if  _P_r_e_d can  not be  applied to a  pair.   See  the
  4629.     example above.
  4630.  
  4631.  
  4632. ssuubblliisstt((_+_P_r_e_d_, _+_L_i_s_t_1_, _?_L_i_s_t_2))
  4633.     Unify  _L_i_s_t_2 with  a list  of all elements  of _L_i_s_t_1  to which  _P_r_e_d
  4634.     applies.
  4635.  
  4636.  
  4637. 33..3300 FFoorraallll
  4638.  
  4639.  
  4640. ffoorraallll((_+_C_o_n_d_, _+_A_c_t_i_o_n))
  4641.     For  all alternative bindings  of _C_o_n_d  _A_c_t_i_o_n can be  proven.   The
  4642.     example  verifies that all arithmetic  statements in the list _L  are
  4643.     correct.  It does not say which is wrong if one proves wrong.
  4644.  
  4645.     ?- forall(member(Result = Formula, [2 = 1 + 1, 4 = 2 * 2]),
  4646.                      Result =:= Formula).
  4647.  
  4648.  
  4649. 33..3311 FFoorrmmaatttteedd WWrriittee
  4650.  
  4651. The  current  version   of  SWI-Prolog  provides  two  formatted   write
  4652. predicates.    The  first  is wwrriitteeff//[[11,,22]],  which  is  compatible  with
  4653. Edinburgh C-Prolog.   The  second is  ffoorrmmaatt//[[11,,22]], which is  compatible
  4654. with Quintus  Prolog.   We hope  the Prolog  community will once  define
  4655. a  standard formatted  write predicate.    If you  want performance  use
  4656. ffoorrmmaatt//[[11,,22]] as this predicate is defined in  C. Otherwise compatibility
  4657. reasons might tell you which predicate to use.
  4658.  
  4659.  
  4660. 33..3311..11 WWrriitteeff
  4661.  
  4662.  
  4663. wwrriittee__llnn((_+_T_e_r_m))
  4664.     Equivalent to write(Term), nl.
  4665.  
  4666.  
  4667. wwrriitteeff((_+_A_t_o_m))
  4668.     Equivalent to writef(Atom, []).
  4669.  
  4670.  
  4671. wwrriitteeff((_+_F_o_r_m_a_t_, _+_A_r_g_u_m_e_n_t_s))
  4672.     Formatted  write.      _F_o_r_m_a_t  is  an  atom  whose  characters  will
  4673.     be   printed.     _F_o_r_m_a_t  may  contain  certain   special  character
  4674.     sequences   which  specify   certain  formatting  and   substitution
  4675.     actions.    _A_r_g_u_m_e_n_t_s then  provides all  the terms  required to  be
  4676.     output.
  4677.  
  4678.     Escape sequences to generate a single special character:
  4679.  
  4680.              __________________________________________________
  4681.              | \n   |Output  a  nemline  character  (see  also |
  4682.              |      |nnll//[[00,,11]])                                 |
  4683.              | \l   |Output a line separator (same as \n)      |
  4684.  
  4685.              | \r   |Output   a   carriage-return    character |
  4686.              |      |(ASCII 13)                                |
  4687.              | \t   |Output the ASCII character TAB (9)        |
  4688.              | \\   |The character \ is output                 |
  4689.              | \%   |The character % is output                 |
  4690.              | \nnn |where <_n_n_n>  is an  integer  (1-3 digits) |
  4691.              |      |the character  with ASCII  code  <_n_n_n> is |
  4692.  
  4693.              |______|output_(NB_:_<_n_n_n>_is_read_as_ddeecciimmaall)____|
  4694.  
  4695.     Note  that \l,  \\bnfmeta{nnn}  and \\  are interpreted  differently
  4696.     when character-escapes are in effect.  See section 2.11.1.1.
  4697.  
  4698.     Escape  sequences to include  arguments from _A_r_g_u_m_e_n_t_s.   Each  time
  4699.     a  %  escape sequence  is found  in _F_o_r_m_a_t  the  next argument  from
  4700.     _A_r_g_u_m_e_n_t_s is formatted according to the specification.
  4701.  
  4702.               _________________________________________________%t
  4703.  
  4704.               | %w  pprriinntt//11 the next item (mnemonic:  term)   |    |
  4705.  
  4706.               | %q  |wwrriittee//11the next item                     |
  4707.  
  4708.               | %d  |wwrriitteeqq//11the next item                    |
  4709.  
  4710.               | %p  |ddiissppllaayy//11the next item                   |
  4711.  
  4712.               |     |pprriinntt//11the next item (identical to %t)   |
  4713.               | %n  |Put  the next item  as a  character (i.e.|
  4714.               |     |it is an ASCII value)                    |
  4715.               | %r  |Write the  next item  N times where  N is|
  4716.               |     |the second item (an integer)             |
  4717.  
  4718.               | %s  |Write the  next item  as a String  (so it|
  4719.               |     |must be a list of characters)            |
  4720.               | %f  |Perform a ttttyyfflluusshh//00 (no items used)     |
  4721.               | %Nc |Write  the   next  item  Centered  in  N |
  4722.               |     |columns.                                 |
  4723.               | %Nl |Write the next  item Left justified in N |
  4724.               |     |columns.                                 |
  4725.  
  4726.               | %Nr |Write the next item Right justified in N |
  4727.               |     |columns.   N is a decimal number with  at|
  4728.               |     |least  one digit.   The  item must  be an|
  4729.               |_____|atom,_integer,_float_or_string.__________|_
  4730.  
  4731.  
  4732. sswwrriitteeff((_-_S_t_r_i_n_g_, _+_F_o_r_m_a_t_, _+_A_r_g_u_m_e_n_t_s))
  4733.     Equivalent to wwrriitteeff//22,  but ``writes'' the result on _S_t_r_i_n_g instead
  4734.     of the current output stream.  Example:
  4735.  
  4736.     ?- swritef(S, '%15L%w', ['Hello', 'World']).
  4737.  
  4738.     S = "Hello          World"
  4739.  
  4740.  
  4741. sswwrriitteeff((_-_S_t_r_i_n_g_, _+_F_o_r_m_a_t))
  4742.     Equivalent to swritef(String, Format, []).
  4743.  
  4744.  
  4745. 33..3311..22 FFoorrmmaatt
  4746.  
  4747.  
  4748. ffoorrmmaatt((_+_F_o_r_m_a_t))
  4749.     Defined as `format(Format) :- format(Format, []).'
  4750.  
  4751.  
  4752. ffoorrmmaatt((_+_F_o_r_m_a_t_, _+_A_r_g_u_m_e_n_t_s))
  4753.     _F_o_r_m_a_t   is  an   atom,   list  of   ASCII  values,   or  a   Prolog
  4754.     string.   _A_r_g_u_m_e_n_t_s  provides the arguments  required by the  format
  4755.     specification.   If  only one argument is  required and this is  not
  4756.     a  list of  ASCII values the  argument need  not be put  in a  list.
  4757.     Otherwise the arguments are put in a list.
  4758.  
  4759.     Special sequences start  with the tilde (~), followed by an optional
  4760.     numeric  argument, followed by a character describing the  action to
  4761.     be  undertaken.  A numeric argument is either a sequence  of digits,
  4762.     representing  a  positive decimal  number, a  sequence `<_c_h_a_r_a_c_t_e_r>,
  4763.     representing  the ASCII value of the character (only useful  for ~t)
  4764.     or  a asterisk (*), in when  the numeric argument is taken  from the
  4765.     next  argument of  the argument  list,  which should  be a  positive
  4766.     integer.  Actions are:
  4767.  
  4768.       ~  Output the tilde itself.
  4769.  
  4770.       a  Output the  next  argument, which  should  be an  atom.    This
  4771.          option is equivalent to ww.  Compatibility reasons only.
  4772.  
  4773.       c  Output the  next argument  as an ASCII  value.   This  argument
  4774.          should be an  integer in the range  [0, ..., 255] (including  0
  4775.          and 255).
  4776.  
  4777.       d  Output  next argument  as  a decimal  number.    It  should  be
  4778.          an integer.    If  a numeric  argument is  specified  a dot  is
  4779.          inserted _a_r_g_u_m_e_n_t  positions from the  right (useful for  doing
  4780.          fixed point arithmetic with integers, such as  handling amounts
  4781.          of money).
  4782.  
  4783.       D  Same as dd, but  makes large values easier to read  by inserting
  4784.          a comma every three digits left to the dot or right.
  4785.  
  4786.       e  Output next argument as a floating point  number in exponential
  4787.          notation.    The  numeric  argument  specifies  the  precision.
  4788.          Default is  6 digits.   Exact representation  depends on the  C
  4789.          library function printf().   This function is invoked  with the
  4790.          format %.<_p_r_e_c_i_s_i_o_n>e.
  4791.  
  4792.       E  Equivalent  to ee,  but  outputs a  capital  E to  indicate  the
  4793.          exponent.
  4794.  
  4795.       f  Floating point  in non-exponential  notation.    See C  library
  4796.          function printf().
  4797.  
  4798.       g  Floating point in ee or ff notation, whichever is shorter.
  4799.  
  4800.       G  Floating point in EE or ff notation, whichever is shorter.
  4801.  
  4802.       i  Ignore  next argument  of  the  argument  list.    Produces  no
  4803.          output.
  4804.  
  4805.       k  Give the next argument to ddiissppllaayyqq//11 (canonical write).
  4806.  
  4807.       n  Output a newline character.
  4808.  
  4809.       N  Only output  a newline  if the  last character  output on  this
  4810.          stream was not a newline.  Not properly implemented yet.
  4811.  
  4812.       p  Give the next argument to pprriinntt//11.
  4813.  
  4814.       q  Give the next argument to wwrriitteeqq//11.
  4815.  
  4816.       r  Print integer  in radix the  numeric argument  notation.   Thus
  4817.          ~16r prints its argument  hexadecimal.  The argument  should be
  4818.          in the range [2; :::;36].   Lower case letters are used for  digits
  4819.          above 9.
  4820.  
  4821.       R  Same as rr, but uses upper case letters for digits above 9.
  4822.  
  4823.       s  Output a string of  ASCII characters or a string  (see ssttrriinngg//11
  4824.          and section 3.20) from the next argument.
  4825.  
  4826.       t  All remaining space between 2 tabs tops  is distributed equally
  4827.          over ~t  statements  between the  tabs  tops.   This  space  is
  4828.          padded with  spaces by  default.   If an  argument is  supplied
  4829.          this is taken to be  the ASCII value of the character  used for
  4830.          padding.   This  can be  used to  do left  or right  alignment,
  4831.          centering, distributing, etc.   See also  ~| and ~+ to set  tab
  4832.          stops.  A tabs top is assumed at the start of each line.
  4833.  
  4834.       |  Set a  tabs top on  the current position.    If an argument  is
  4835.          supplied set  a  tabs top  on the  position  of that  argument.
  4836.          This  will  cause  all  ~t's  to  be  distributed  between  the
  4837.          previous and this tabs top.
  4838.  
  4839.       +  Set a tabs top relative  to the current position.   Further the
  4840.          same as ~|.
  4841.  
  4842.       w  Give the next argument to wwrriittee//11.
  4843.  
  4844.     Example:
  4845.  
  4846.     simple_statistics :-
  4847.         <obtain statistics>         % left to the user
  4848.         format('~tStatistics~t~72|~n~n'),
  4849.         format('Runtime: ~`.t ~2f~34|  Inferences: ~`.t ~D~72|~n',
  4850.                                                 [RunT, Inf]),
  4851.         ....
  4852.  
  4853.     Will output
  4854.  
  4855.                                  Statistics
  4856.  
  4857.     Runtime: .................. 3.45  Inferences: .......... 60,345
  4858.  
  4859.  
  4860. ffoorrmmaatt((_+_S_t_r_e_a_m_, _+_F_o_r_m_a_t_, _+_A_r_g_u_m_e_n_t_s))
  4861.     As ffoorrmmaatt//22, but write the output on the given _S_t_r_e_a_m.
  4862.  
  4863.  
  4864. ssffoorrmmaatt((_-_S_t_r_i_n_g_, _+_F_o_r_m_a_t_, _+_A_r_g_u_m_e_n_t_s))
  4865.     Equivalent to ffoorrmmaatt//22,  but ``writes'' the result on _S_t_r_i_n_g instead
  4866.     of the current output stream.  Example:
  4867.  
  4868.     ?- sformat(S, '~w~t~15|~w', ['Hello', 'World']).
  4869.  
  4870.     S = "Hello          World"
  4871.  
  4872.  
  4873. ssffoorrmmaatt((_-_S_t_r_i_n_g_, _+_F_o_r_m_a_t))
  4874.     Equivalent to `sformat(String, Format, []).'
  4875.  
  4876.  
  4877. 33..3311..33 PPrrooggrraammmmiinngg FFoorrmmaatt
  4878.  
  4879.  
  4880. ffoorrmmaatt__pprreeddiiccaattee((_+_C_h_a_r_, _+_H_e_a_d))
  4881.     If  a sequence ~c (tilde, followed by some character) is  found, the
  4882.     format  derivatives will first check whether the user has  defined a
  4883.     predicate  to handle the format.   If  not, the built in  formatting
  4884.     rules  described above are  used.   _C_h_a_r is  either an ascii  value,
  4885.     or  a one character atom,  specifying the letter to be  (re)defined.
  4886.     _H_e_a_d  is a  term, whose  name and  arity are used  to determine  the
  4887.     predicate  to call  for  the redefined  formatting character.    The
  4888.     first  argument to  the  predicate is  the numeric  argument of  the
  4889.     format  command, or the  atom default if  no argument is  specified.
  4890.     The  remaining arguments  are filled from  the argument  list.   The
  4891.     example  below redefines ~n to produce _A_r_g times return  followed by
  4892.     linefeed (so a (Grr.)  DOS machine is happy with the output).
  4893.  
  4894.     :- format_predicate(n, dos_newline(_Arg)).
  4895.  
  4896.     dos_newline(Arg) :-
  4897.             between(1, Ar, _), put(13), put(10), fail ; true.
  4898.  
  4899.  
  4900. 33..3322 TTeerrmmiinnaall CCoonnttrrooll
  4901.  
  4902. The  following  predicates  form  a  simple  access   mechanism  to  the
  4903. Unix  termcap library  to provide  terminal independent  I/O for  screen
  4904. terminals.   The  library package  library(tty) builds on  top of  these
  4905. predicates.
  4906.  
  4907.  
  4908. ttttyy__ggeett__ccaappaabbiilliittyy((_+_N_a_m_e_, _+_T_y_p_e_, _-_R_e_s_u_l_t))
  4909.     Get  the  capability named  _N_a_m_e  from the  termcap  library.    See
  4910.     termcap(5)  for the capability  names.   _T_y_p_e specifies the type  of
  4911.     the  expected result, and is one of string, number or bool.   String
  4912.     results  are returned as  an atom, number  result as an integer  and
  4913.     bool  results as the atom on or off.   If an option cannot  be found
  4914.     this predicate fails  silently.  The results are only computed once.
  4915.     Successive queries on the same capability are fast.
  4916.  
  4917.  
  4918. ttttyy__ggoottoo((_+_X_, _+_Y))
  4919.     Goto  position  (_X, _Y) on  the screen.    Note  that the  predicates
  4920.     lliinnee__ccoouunntt//22  and  lliinnee__ppoossiittiioonn//22 will  not  have  a  well  defined
  4921.     behaviour while using this predicate.
  4922.  
  4923.  
  4924. ttttyy__ppuutt((_+_A_t_o_m_, _+_L_i_n_e_s))
  4925.     Put  an  atom  via the  termcap  library  function tputs().     This
  4926.     function  decodes padding  information  in the  strings returned  by
  4927.     ttttyy__ggeett__ccaappaabbiilliittyy//33 and should  be used  to  output these  strings.
  4928.     _L_i_n_e_s is the number  of lines affected by the operation, or 1 if not
  4929.     applicable (as in almost all cases).
  4930.  
  4931.  
  4932. sseett__ttttyy((_-_O_l_d_S_t_r_e_a_m_, _+_N_e_w_S_t_r_e_a_m))
  4933.     Set  the  output  stream,  used  by  ttttyy__ppuutt//22 and  ttttyy__ggoottoo//22 to  a
  4934.     specific stream.  Default is user_output.
  4935.  
  4936.  
  4937. 33..3333 OOppeerraattiinngg SSyysstteemm IInntteerraaccttiioonn
  4938.  
  4939.  
  4940. sshheellll((_+_C_o_m_m_a_n_d_, _-_S_t_a_t_u_s))
  4941.     Execute  _C_o_m_m_a_n_d on the operating system.   _C_o_m_m_a_n_d is given to  the
  4942.     Bourne  shell (/bin/sh).  _S_t_a_t_u_s is unified with the exit  status of
  4943.     the command.
  4944.  
  4945.     On  _W_i_n_3_2  systems,  sshheellll//[[11,,22]]  executes  the  command  using  the
  4946.     CreateProcess() API and waits  for the command to terminate.  If the
  4947.     command  ends with a & sign, the command is handed to  the WinExec()
  4948.     API,  which does not wait for the  new task to terminate.   See also
  4949.     wwiinn__eexxeecc//22.
  4950.  
  4951.  
  4952. sshheellll((_+_C_o_m_m_a_n_d))
  4953.     Equivalent to `shell(Command, 0)'.
  4954.  
  4955.  
  4956. sshheellll
  4957.     Start  an  interactive  Unix  shell.     Default  is  /bin/sh,   the
  4958.     environment  variable SHELL overrides this  default.  Not  available
  4959.     for Win32 platforms.
  4960.  
  4961.  
  4962. wwiinn__eexxeecc((_+_C_o_m_m_a_n_d_, _+_S_h_o_w))
  4963.     Win32  systems only.    Spawns a  Windows task  without waiting  for
  4964.     its  completion.  _S_h_o_w is  either iconic or normal and dictates  the
  4965.     initial  status of the window.   The iconic option is notably  handy
  4966.     to start (DDE) servers.
  4967.  
  4968.  
  4969. ggeetteennvv((_+_N_a_m_e_, _-_V_a_l_u_e))
  4970.     Get Unix environment variable  (see csh(1) and sh(1)).  Fails if the
  4971.     variable does not exist.
  4972.  
  4973.  
  4974. sseetteennvv((_+_N_a_m_e_, _+_V_a_l_u_e))
  4975.     Set   Unix  environment  variable.     _N_a_m_e  and  _V_a_l_u_e   should  be
  4976.     instantiated  to atoms or integers.   The environment variable  will
  4977.     be passed to sshheellll//[[00--22]] and can be requested using ggeetteennvv//22.
  4978.  
  4979.  
  4980. uunnsseetteennvv((_+_N_a_m_e))
  4981.     Remove Unix environment variable from the environment.
  4982.  
  4983.  
  4984. ggeett__ttiimmee((_-_T_i_m_e))
  4985.     Return  the number of seconds that elapsed since the epoch  of Unix,
  4986.     1  January 1970, 0  hours.  _T_i_m_e  is a floating  point number.   Its
  4987.     granularity is system dependent.  On sun, this is 1/60 of a second.
  4988.  
  4989.  
  4990. ccoonnvveerrtt__ttiimmee((_+_T_i_m_e_, _-_Y_e_a_r_, _-_M_o_n_t_h_, _-_D_a_y_, _-_H_o_u_r_, _-_M_i_n_u_t_e_, _-_S_e_c_o_n_d_, _-_M_i_l_l_i_S_e_c_o_n_d_s))
  4991.  
  4992.     Convert   a  time   stamp,  provided   by  ggeett__ttiimmee//11,  ttiimmee__ffiillee//22,
  4993.     etc.   _Y_e_a_r is  unified with the year,  _M_o_n_t_h with the month  number
  4994.     (January  is 1), _D_a_y  with the day of  the month (starting with  1),
  4995.     _H_o_u_r  with  the hour  of the  day (0--23),  _M_i_n_u_t_e  with the  minute
  4996.     (0--59).   _S_e_c_o_n_d with the  second (0--59) and _M_i_l_l_i_S_e_c_o_n_d with  the
  4997.     milliseconds  (0--999).  Note that the latter might not  be accurate
  4998.     or  might always be 0, depending  on the timing capabilities of  the
  4999.     system.
  5000.  
  5001.  
  5002. 33..3344 FFiillee SSyysstteemm IInntteerraaccttiioonn
  5003.  
  5004.  
  5005. aacccceessss__ffiillee((_+_F_i_l_e_, _+_M_o_d_e))
  5006.     Succeeds  if _F_i_l_e exists and can be accessed by this  prolog process
  5007.     under  mode _M_o_d_e.   _M_o_d_e is  one of the  atoms read, write,  append,
  5008.     exist,  none or execute.  _F_i_l_e may also be the name  of a directory.
  5009.     Fails  silently otherwise.   access_file(File, none)simply  succeeds
  5010.     without testing anything.
  5011.  
  5012.     If  `Mode' is write or append,  this predicate also succeeds if  the
  5013.     file  does not exist and the user has write-access to  the directory
  5014.     of the specified location.
  5015.  
  5016.  
  5017. eexxiissttss__ffiillee((_+_F_i_l_e))
  5018.     Succeeds  when _F_i_l_e exists.  This  does not imply the user  has read
  5019.     and/or write permission for the file.
  5020.  
  5021.  
  5022. ffiillee__ddiirreeccttoorryy__nnaammee((_+_F_i_l_e_, _-_D_i_r_e_c_t_o_r_y))
  5023.     Extracts  the directory-part of _F_i_l_e.  The resulting  _D_i_r_e_c_t_o_r_y name
  5024.     ends  with the directory separator character /.  If _F_i_l_e is  an atom
  5025.     that does not  contain any directory separator characters, the empty
  5026.     atom '' is returned.  See also ffiillee__bbaassee__nnaammee//22.
  5027.  
  5028.  
  5029. ffiillee__bbaassee__nnaammee((_+_F_i_l_e_, _-_B_a_s_e_N_a_m_e))
  5030.     Extracts the filename part  from a path specification.  If _F_i_l_e does
  5031.     not contain any directory separators, _F_i_l_e is returned.
  5032.  
  5033.  
  5034. ssaammee__ffiillee((_+_F_i_l_e_1_, _+_F_i_l_e_2))
  5035.     Succeeds  if both filenames refer to  the same physical file.   That
  5036.     is,  if _F_i_l_e_1  and _F_i_l_e_2  are the  same string or  both names  exist
  5037.     and  point to the  same file (due to  hard or symbolic links  and/or
  5038.     relative vs.  absolute paths).
  5039.  
  5040.  
  5041. eexxiissttss__ddiirreeccttoorryy((_+_D_i_r_e_c_t_o_r_y))
  5042.     Succeeds  if _D_i_r_e_c_t_o_r_y exists.    This does not  imply the user  has
  5043.     read, search and or write permission for the directory.
  5044.  
  5045.  
  5046. ddeelleettee__ffiillee((_+_F_i_l_e))
  5047.     Unlink _F_i_l_e from the Unix file system.
  5048.  
  5049.  
  5050. rreennaammee__ffiillee((_+_F_i_l_e_1_, _+_F_i_l_e_2))
  5051.     Rename  _F_i_l_e_1 into _F_i_l_e_2.   Currently  files cannot be moved  across
  5052.     devices.
  5053.  
  5054.  
  5055. ssiizzee__ffiillee((_+_F_i_l_e_, _-_S_i_z_e))
  5056.     Unify _S_i_z_e with the size of _F_i_l_e in characters.
  5057.  
  5058.  
  5059. ttiimmee__ffiillee((_+_F_i_l_e_, _-_T_i_m_e))
  5060.     Unify  the last  modification time of  _F_i_l_e with  _T_i_m_e.   _T_i_m_e is  a
  5061.     floating  point number expressing the  seconds elapsed since Jan  1,
  5062.     1970.
  5063.  
  5064.  
  5065. aabbssoolluuttee__ffiillee__nnaammee((_+_F_i_l_e_, _-_A_b_s_o_l_u_t_e))
  5066.     Expand  Unix file specification  into an absolute  path.  User  home
  5067.     directory  expansion (~ and ~<_u_s_e_r>) and variable expansion  is done.
  5068.     The  absolute  path  is canonised:    references  to .  and  ..  are
  5069.     deleted.   SWI-Prolog  uses absolute file  names to register  source
  5070.     files  independent  of the  current  working directory.    See  also
  5071.     aabbssoolluuttee__ffiillee__nnaammee//33.
  5072.  
  5073.  
  5074. aabbssoolluuttee__ffiillee__nnaammee((_+_S_p_e_c_, _+_O_p_t_i_o_n_s_, _-_A_b_s_o_l_u_t_e))
  5075.     Converts  the  given  file  specification  into  an  absolute  path.
  5076.     _O_p_t_i_o_n is a list of options to guide the conformation process:
  5077.  
  5078.     eexxtteennssiioonnss((_L_i_s_t_O_f_E_x_t_e_n_s_i_o_n_s))
  5079.          List of  file-extensions to  try.   Default is  ''.   For  each
  5080.          extension, aabbssoolluuttee__ffiillee__nnaammee//33will first add the extension and
  5081.          then verify the  conditions imposed by the  other options.   If
  5082.          the condition fails, the  next extension of the list  is tried.
  5083.          Extensions may be specified both as ..ext or plain ext.
  5084.  
  5085.     aacccceessss((_M_o_d_e))
  5086.          Imposes the condition  access_file(_F_i_l_e, _M_o_d_e).   _M_o_d_e is on  of
  5087.          read, write, append, exist or none.  See also aacccceessss__ffiillee//22.
  5088.  
  5089.     ffiillee__ttyyppee((_T_y_p_e))
  5090.          Defines  extensions.    Current  mapping:   txt  implies  [''],
  5091.          prolog  implies ['.pl', ''],  executable  implies  ['.so', ''],
  5092.          qlf implies ['.qlf', ''] and directory implies [''].
  5093.  
  5094.     ffiillee__eerrrroorrss((_f_a_i_l_/_t_r_u_e))
  5095.          Report if  the  path cannot  be resolved  or be  silent.    The
  5096.          default is to stay silent.
  5097.  
  5098.     ssoolluuttiioonnss((_f_i_r_s_t_/_a_l_l))
  5099.          If  first (default),  the  predicates leaves  no  choice-point.
  5100.          Otherwise a  choice-point  will be  left and  backtracking  may
  5101.          yield more solutions.
  5102.  
  5103.  
  5104. iiss__aabbssoolluuttee__ffiillee__nnaammee((_+_F_i_l_e))
  5105.     True  if _F_i_l_e specifies  and absolute path-name.   On Unix  systems,
  5106.     this   implies  the  path  starts  with  a  `/'.      For  Microsoft
  5107.     based   systems  this  implies  the   path  starts  with  <_l_e_t_t_e_r>:.
  5108.     This   predicate   is  intended   to  provide   platform-independent
  5109.     checking  for  absolute paths.    See  also aabbssoolluuttee__ffiillee__nnaammee//22 and
  5110.     pprroolloogg__ttoo__ooss__ffiilleennaammee//22.
  5111.  
  5112.  
  5113. ffiillee__nnaammee__eexxtteennssiioonn((_?_B_a_s_e_, _?_E_x_t_e_n_s_i_o_n_, _?_N_a_m_e))
  5114.     This  predicate is used to add, remove or test  filename extensions.
  5115.     The  main reason  for  its introduction  is to  deal with  different
  5116.     filename  properties  in a  portable manner.    If  the file  system
  5117.     is   case-insensitive,  testing  for  an  extension  will   be  done
  5118.     case-insensitive too.   _E_x_t_e_n_s_i_o_n may be specified with or without a
  5119.     leading  dot (.).  If an _E_x_t_e_n_s_i_o_n is generated, it will  not have a
  5120.     leading dot.
  5121.  
  5122.  
  5123. eexxppaanndd__ffiillee__nnaammee((_+_W_i_l_d_C_a_r_d_, _-_L_i_s_t))
  5124.     Unify  _L_i_s_t with  a  sorted list  of files  or directories  matching
  5125.     _W_i_l_d_C_a_r_d.   The  normal Unix wildcard  constructs `?', `*',  `[...]'
  5126.     and  `{...}'  are  recognised.    The interpretation  of  `{...}'  is
  5127.     interpreted  slightly different  from  the C  shell (csh(1)).    The
  5128.     comma  separated  argument  can  be  arbitrary  patterns,  including
  5129.     `{...}'  patterns.   The empty  pattern is legal  as well:   `{.pl,}'
  5130.     matches either `.pl' or the empty string.
  5131.  
  5132.  
  5133. pprroolloogg__ttoo__ooss__ffiilleennaammee((_?_P_r_o_l_o_g_P_a_t_h_, _?_O_s_P_a_t_h))
  5134.     Converts  between the internal  Prolog pathname conventions and  the
  5135.     operating-system  pathname conventions.    The internal  conventions
  5136.     are  Unix and this predicates is  equivalent to =/2 (unify) on  Unix
  5137.     systems.   On  DOS systems it  will change the  directory-separator,
  5138.     limit  the filename length map dots,  except for the last one,  onto
  5139.     underscores.
  5140.  
  5141.  
  5142. rreeaadd__lliinnkk((_+_F_i_l_e_, _-_L_i_n_k_, _-_T_a_r_g_e_t))
  5143.     If _F_i_l_e points to  a symbolic link, unify _L_i_n_k with the value of the
  5144.     link and _T_a_r_g_e_t to  the file the link is pointing to.  _T_a_r_g_e_t points
  5145.     to  a file, directory or non-existing entry in the file  system, but
  5146.     never  to a link.   Fails if _F_i_l_e  is not a link.   Fails always  on
  5147.     systems that do not support symbolic links.
  5148.  
  5149.  
  5150. ttmmpp__ffiillee((_+_B_a_s_e_, _-_T_m_p_N_a_m_e))
  5151.     Create  a name for a temporary file.  _B_a_s_e is an  identifier for the
  5152.     category  of file.  The _T_m_p_N_a_m_e is guaranteed to be unique.   If the
  5153.     system  halts, it  will automatically  remove all created  temporary
  5154.     files.
  5155.  
  5156.  
  5157. cchhddiirr((_+_P_a_t_h))
  5158.     Change working directory to _P_a_t_h.
  5159.  
  5160.  
  5161. 33..3355 UUsseerr TToopplleevveell MMaanniippuullaattiioonn
  5162.  
  5163.  
  5164. bbrreeaakk
  5165.     Recursively  start a new  Prolog top level.   This Prolog top  level
  5166.     has its own  stacks, but shares the heap with all break environments
  5167.     and  the top level.  Debugging  is switched off on entering  a break
  5168.     and  restored on leaving one.   The break environment is  terminated
  5169.     by  typing the  system's end-of-file character  (control-D). If  the
  5170.     -t toplevel  command  line  option is  given  this goal  is  started
  5171.     instead of entering the default interactive top level (pprroolloogg//00).
  5172.  
  5173.  
  5174. aabboorrtt
  5175.     Abort  the Prolog  execution and  start a  new top  level.   If  the
  5176.     -t toplevel  command  line options  is given  this  goal is  started
  5177.     instead  of  entering the  default  interactive top  level.    Break
  5178.     environments  are aborted as  well.  All  open files except for  the
  5179.     terminal  related files are  closed.   The input- and output  stream
  5180.     again refers to _u_s_e_r.
  5181.  
  5182.  
  5183. hhaalltt
  5184.     Terminate  Prolog  execution.   Open  files are  closed  and if  the
  5185.     command  line option  -tty is  not active the  terminal status  (see
  5186.     Unix  stty(1)) is restored.  Hooks may be registered both  in Prolog
  5187.     and  in foreign code.  Prolog  hooks are registered using aatt__hhaalltt//11.
  5188.     hhaalltt//00 is equivalent to halt(0).
  5189.  
  5190.  
  5191. hhaalltt((_+_S_t_a_t_u_s))
  5192.     Terminate  Prolog  execution  with  given  status.    Status  is  an
  5193.     integer.  See also hhaalltt//00.
  5194.  
  5195.  
  5196. pprroolloogg
  5197.     This  goal starts the  default interactive top  level.  Queries  are
  5198.     read  from the  stream user_input.    See also  the history  feature
  5199.     (ffeeaattuurree//22).    The pprroolloogg//00 predicate  is terminated (succeeds)  by
  5200.     typing the end-of-file character (Unix:  control-D).
  5201.  
  5202. The following  two hooks allow  for expanding  queries and handling  the
  5203. result  of a  query.   These  hooks are  used by  the toplevel  variable
  5204. expansion mechanism described in section 2.5.
  5205.  
  5206.  
  5207. eexxppaanndd__qquueerryy((_+_Q_u_e_r_y_, _-_E_x_p_a_n_d_e_d_, _+_B_i_n_d_i_n_g_s_, _-_E_x_p_a_n_d_e_d_B_i_n_d_i_n_g_s))
  5208.     Hook  in module  user, normally  not defined.    _Q_u_e_r_y and  _B_i_n_d_i_n_g_s
  5209.     represents  the  query read  from  the user  and  the names  of  the
  5210.     free  variables as  obtained using rreeaadd__tteerrmm//33.   If this  predicate
  5211.     succeeds, it should  bind _E_x_p_a_n_d_e_d and _E_x_p_a_n_d_e_d_B_i_n_d_i_n_g_s to the query
  5212.     and  bindings  to be  executed  by the  toplevel.    This  predicate
  5213.     is  used by the toplevel  (pprroolloogg//00).   See also eexxppaanndd__aannsswweerr//22 and
  5214.     tteerrmm__eexxppaannssiioonn//22.
  5215.  
  5216.  
  5217. eexxppaanndd__aannsswweerr((_+_B_i_n_d_i_n_g_s_, _-_E_x_p_a_n_d_e_d_B_i_n_d_i_n_g_s))
  5218.     Hook  in module user,  normally not defined.   Expand the result  of
  5219.     a  successfully executed  toplevel  query.   _B_i_n_d_i_n_g_s  is the  query
  5220.     <_N_a_m_e>= <_V_a_l_u_e>binding list  from the query.  _E_x_p_a_n_d_e_d_B_i_n_d_i_n_g_s must
  5221.     be unified with the bindings the toplevel should print.
  5222.  
  5223.  
  5224. 33..3366 CCrreeaattiinngg aa PPrroottooccooll ooff tthhee UUsseerr IInntteerraaccttiioonn
  5225.  
  5226. SWI-Prolog offers the  possibility to log the interaction with  the user
  5227. on  a file.    All  Prolog interaction,  including warnings  and  tracer
  5228. output, are written on the protocol file.
  5229.  
  5230.  
  5231. pprroottooccooll((_+_F_i_l_e))
  5232.     Start  protocolling on file  _F_i_l_e.  If  there is already a  protocol
  5233.     file open then close it first.  If _F_i_l_e exists it is truncated.
  5234.  
  5235.  
  5236. pprroottooccoollaa((_+_F_i_l_e))
  5237.     Equivalent  to pprroottooccooll//11,  but  does not  truncate the  _F_i_l_e if  it
  5238.     exists.
  5239.  
  5240.  
  5241. nnoopprroottooccooll
  5242.     Stop  making a protocol of the user interaction.  Pending  output is
  5243.     flushed on the file.
  5244.  
  5245.  
  5246. pprroottooccoolllliinngg((_-_F_i_l_e))
  5247.     Succeeds  if a protocol was  started with pprroottooccooll//11 or  pprroottooccoollaa//11
  5248.     and unifies _F_i_l_e with the current protocol output file.
  5249.  
  5250.  
  5251. 33..3377 DDeebbuuggggiinngg aanndd TTrraacciinngg PPrrooggrraammss
  5252.  
  5253.  
  5254. ttrraaccee
  5255.     Start  the tracer.   ttrraaccee//00  itself cannot be  seen in the  tracer.
  5256.     Note  that  the Prolog  toplevel treats  ttrraaccee//00 special;  it  means
  5257.     `trace the next goal'.
  5258.  
  5259.  
  5260. ttrraacciinngg
  5261.     Succeeds  when  the tracer  is  currently switched  on.    ttrraacciinngg//00
  5262.     itself can not be seen in the tracer.
  5263.  
  5264.  
  5265. nnoottrraaccee
  5266.     Stop the tracer.  nnoottrraaccee//00 itself cannot be seen in the tracer.
  5267.  
  5268.  
  5269. ttrraaccee((_+_P_r_e_d))
  5270.     Equivalent to trace(Pred, +all).
  5271.  
  5272.  
  5273. ttrraaccee((_+_P_r_e_d_, _+_P_o_r_t_s))
  5274.     Put  a  trace-point  on  all  predicates  satisfying  the  predicate
  5275.     specification  _P_r_e_d.   _P_o_r_t_s  is a  list of  portnames (call,  redo,
  5276.     exit,  fail).   The atom all refers  to all ports.   If the port  is
  5277.     preceded  by a - sign the trace-point  is cleared for the port.   If
  5278.     it is preceded by a + the trace-point is set.
  5279.  
  5280.     The  predicate ttrraaccee//22  activates debug  mode (see ddeebbuugg//00).    Each
  5281.     time  a port (of the 4-port model) is passed that has  a trace-point
  5282.     set  the goal is printed as  with ttrraaccee//00.  Unlike ttrraaccee//00  however,
  5283.     the  execution is continued without asking for  further information.
  5284.     Examples:
  5285.  
  5286.         ?- trace(hello).         Trace all  ports of hello  with any
  5287.                                  arity in any module.
  5288.         ?- trace(foo/2, +fail).  Trace  failures  of  foo/2  in  any
  5289.                                  module.
  5290.         ?- trace(bar/1, -all).   Stop tracing bar/1.
  5291.  
  5292.     The predicate ddeebbuuggggiinngg//00 shows all currently defined trace-points.
  5293.  
  5294.  
  5295. nnoottrraaccee((_+_G_o_a_l))
  5296.     Call  _G_o_a_l,  but  suspend  the  debugger while  _G_o_a_l  is  executing.
  5297.     The  current  implementation cuts  the  choicepoints of  _G_o_a_l  after
  5298.     successful completion.   See oonnccee//11.  Later implementations may have
  5299.     the same semantics as ccaallll//11.
  5300.  
  5301.  
  5302. ddeebbuugg
  5303.     Start debugger (stop at spy points).
  5304.  
  5305.  
  5306. nnooddeebbuugg
  5307.     Stop debugger (do not trace, nor stop at spy points).
  5308.  
  5309.  
  5310. ddeebbuuggggiinngg
  5311.     Print debug status and spy points on current output stream.
  5312.  
  5313.  
  5314. ssppyy((_+_P_r_e_d))
  5315.     Put   a  spy   point  on  all   predicates  meeting  the   predicate
  5316.     specification _P_r_e_d.  See section 3.3.
  5317.  
  5318.  
  5319. nnoossppyy((_+_P_r_e_d))
  5320.     Remove   spy  point  from  all  predicates  meeting   the  predicate
  5321.     specification _P_r_e_d.
  5322.  
  5323.  
  5324. nnoossppyyaallll
  5325.     Remove all spy points from the entire program.
  5326.  
  5327.  
  5328. lleeaasshh((_?_P_o_r_t_s))
  5329.     Set/query leashing (ports  which allow for user interaction).  _P_o_r_t_s
  5330.     is  one of _+_N_a_m_e, _-_N_a_m_e,  _?_N_a_m_e or a list  of these.  _+_N_a_m_e  enables
  5331.     leashing  on that  port,  _-_N_a_m_e disables  it and  _?_N_a_m_e succeeds  or
  5332.     fails  according to  the  current setting.    Recognised ports  are:
  5333.     call, redo, exit,  fail and unify.  The special shorthand all refers
  5334.     to  all ports, full  refers to all ports  except for the unify  port
  5335.     (default).  half refers to the call, redo and fail port.
  5336.  
  5337.  
  5338. vviissiibbllee((_+_P_o_r_t_s))
  5339.     Set the ports shown  by the debugger.  See lleeaasshh//11 for a description
  5340.     of the port specification.  Default is full.
  5341.  
  5342.  
  5343. uunnkknnoowwnn((_-_O_l_d_, _+_N_e_w))
  5344.     Unify  _O_l_d  with  the current  value  of  the unknown  system  flag.
  5345.     On  success  _N_e_w  will be  used  to specify  the  new  value.    _N_e_w
  5346.     should  be  instantiated  to either  fail  or trace  and  determines
  5347.     the  interpreters action  when an undefined  predicate which is  not
  5348.     declared  dynamic  is encountered  (see ddyynnaammiicc//11).    fail  implies
  5349.     the  predicate  just  fails silently.     trace implies  the  tracer
  5350.     is  started.    Default is  trace.   The  unknown flag  is local  to
  5351.     each  module and uunnkknnoowwnn//22  is module  transparent.   Using it as  a
  5352.     directive  in a module  file will only  change the unknown flag  for
  5353.     that  module.   Using the  :/2 construct  the behaviour on  trapping
  5354.     an  undefined predicate can  be changed for any  module.  Note  that
  5355.     if  the unknown flag  for a module equals  fail the system will  not
  5356.     call  eexxcceeppttiioonn//33 and will nnoott try to resolve the predicate  via the
  5357.     dynamic  library system.   The system will  still try to import  the
  5358.     predicate from the public module.
  5359.  
  5360.  
  5361. ssttyyllee__cchheecckk((_+_S_p_e_c))
  5362.     Set  style checking options.   _S_p_e_c  is either  +<_o_p_t_i_o_n>, -<_o_p_t_i_o_n>,
  5363.     ?<_o_p_t_i_o_n>  or a  list  of such  options.    +<_o_p_t_i_o_n> sets  a  style
  5364.     checking option,  -<_o_p_t_i_o_n>clears it and ?<_o_p_t_i_o_n> succeeds or fails
  5365.     according to the  current setting.  ccoonnssuulltt//11 and derivatives resets
  5366.     the  style checking options to their value before loading  the file.
  5367.     If---for  example---a file  containing long atoms  should be  loaded
  5368.     the user can start the file with:
  5369.  
  5370.     :- style_check(-atom).
  5371.  
  5372.     Currently available options are:
  5373.  
  5374.     ____________________________________________________________________
  5375.     |_Name__________|Default_|Description_______________________________||singletonon|  ||
  5376.  
  5377.     |                         rreeaadd__ccllaauussee//11 (used by  ccoonnssuulltt//11)  warns |               ||
  5378.     |                         on variables  only  appearing once  in  a |               ||
  5379.     |                         term  (clause)  which  have  a  name  not |               ||
  5380.  
  5381.     ||atom          |  on    |starting with an underscore.              ||              ||
  5382.  
  5383.     |               |        |rreeaadd//11 and  derivatives will  produce  an |
  5384.     |               |        |error message on quoted atoms or  strings |
  5385.     |               |        |longer than 5 lines.                      |
  5386.     | dollar        |  off   |Accept dollar as a lower case  character, |
  5387.     |               |        |thus avoiding the need for quoting  atoms |
  5388.     |               |        |with dollar  signs.   System  maintenance |
  5389.     |               |        |use only.                                 |
  5390.     | discontiguous |  on    |Warn if the clauses  for a predicate  are |
  5391.  
  5392.     |               |        |not together in the same source file.     |
  5393.     | string        |  off   |Read  and  derivatives  transform   "..." |
  5394.     |               |        |into a prolog  string instead  of a  list |
  5395.     |_______________|________|of_ASCII_characters.______________________|
  5396.  
  5397.  
  5398. 33..3388 OObbttaaiinniinngg RRuunnttiimmee SSttaattiissttiiccss
  5399.  
  5400.  
  5401. ssttaattiissttiiccss((_+_K_e_y_, _-_V_a_l_u_e))
  5402.     Unify system statistics  determined by _K_e_y with _V_a_l_u_e.  The possible
  5403.     keys are given in the table 3.2.
  5404.      _______________________________________________________________
  5405.      | cputime     |(User)  cpu time  since Prolog  was started  in|
  5406.      |             |seconds                                        |
  5407.      | inferences  |Total number  of passes via  the call and  redo|
  5408.      |             |ports since Prolog was started.                |
  5409.      | heap        |Estimated   total  size   of   the  heap   (see|
  5410.  
  5411.      |             |section 2.12.1.1)                              |
  5412.      | heapused    |Bytes heap in use by Prolog.                   |
  5413.      | heaplimit   |Maximum   size   of   the    heap   (see   sec-|
  5414.      |             |tion 2.12.1.1)                                 |
  5415.      | local       |Allocated size of the local stack in bytes.    |
  5416.      | localused   |Number of bytes in use on the local stack.     |
  5417.      | locallimit  |Size to  which the  local stack  is allowed  to|
  5418.      |             |grow                                           |
  5419.  
  5420.      | global      |Allocated size of the global stack in bytes.   |
  5421.      | globalused  |Number of bytes in use on the global stack.    |
  5422.      | globallimit |Size to  which the global  stack is allowed  to|
  5423.      |             |grow                                           |
  5424.      | trail       |Allocated size of the trail stack in bytes.    |
  5425.      | trailused   |Number of bytes in use on the trail stack.     |
  5426.      | traillimit  |Size to  which the  trail stack  is allowed  to|
  5427.  
  5428.      |             |grow                                           |
  5429.      | atoms       |Total number of defined atoms.                 |
  5430.      | functors    |Total number of defined name/arity pairs.      |
  5431.      | predicates  |Total number of predicate definitions.         |
  5432.      | modules     |Total number of module definitions.            |
  5433.      |_codes_______|Total_amount_of_byte_codes_in_all_clauses._____|_
  5434.  
  5435.                    Table 3.2:  Keys for ssttaattiissttiiccss//22
  5436.  
  5437.  
  5438. ssttaattiissttiiccss
  5439.     Display a table of system statistics on the current output stream.
  5440.  
  5441.  
  5442. ttiimmee((_+_G_o_a_l))
  5443.     Execute  _G_o_a_l  just like  oonnccee//11 (i.e.  leaving  no choice  points),
  5444.     but  print used time, number  of logical inferences and the  average
  5445.     number  of  _l_i_p_s  (logical  inferences  per  second).     Note  that
  5446.     SWI-Prolog  counts the actual  executed number of inferences  rather
  5447.     than  the number of passes through  the call- and redo ports of  the
  5448.     theoretical 4-port model.
  5449.  
  5450.  
  5451. 33..3399 FFiinnddiinngg PPeerrffoorrmmaannccee BBoottttlleenneecckkss
  5452.  
  5453. SWI-Prolog  offers  a  statistical  program  profiler  similar  to  Unix
  5454. prof(1) for C  and some other languages.   A profiler is used as  an aid
  5455. to find performance  pigs in programs.   It provides information on  the
  5456. time spent in the various Prolog predicates.
  5457.  
  5458. The  profiler  is  based  on the  assumption  that  if  we  monitor  the
  5459. functions on  the execution stack  on time  intervals not correlated  to
  5460. the program's execution the  number of times we find a procedure  on the
  5461. environment stack  is a  measure of  the time spent  in this  procedure.
  5462. It  is implemented  by calling  a procedure  each time  slice Prolog  is
  5463. active.   This procedure  scans the local stack  and either just  counts
  5464. the procedure on top  of this stack (plain profiling) or  all procedures
  5465. on  the stack  (cumulative profiling).    To get  accurate results  each
  5466. procedure  one is  interested  in should  have  a reasonable  number  of
  5467. counts.    Typically  a  minute runtime  will  suffice  to get  a  rough
  5468. overview of the most expensive procedures.
  5469.  
  5470.  
  5471. pprrooffiillee((_+_G_o_a_l_, _+_S_t_y_l_e_, _+_N_u_m_b_e_r))
  5472.     Execute  _G_o_a_l  just  like  ttiimmee//11.    Collect  profiling  statistics
  5473.     according  to  style  (see  pprrooffiilleerr//22)  and  show  the  top  _N_u_m_b_e_r
  5474.     procedures  on  the  current  output  stream  (see  sshhooww__pprrooffiillee//11).
  5475.     The  results  are kept  in  the database  until  rreesseett__pprrooffiilleerr//00 or
  5476.     pprrooffiillee//33  is called and can be displayed again with sshhooww__pprrooffiillee//11.
  5477.     pprrooffiillee//33 is the normal  way to invoke the profiler.  The predicates
  5478.     below are low-level predicates that can be used for special cases.
  5479.  
  5480.  
  5481. sshhooww__pprrooffiillee((_+_N_u_m_b_e_r))
  5482.     Show  the collected  results of the  profiler.   Stops the  profiler
  5483.     first  to avoid interference from sshhooww__pprrooffiillee//11.  It shows  the top
  5484.     _N_u_m_b_e_r predicates according the percentage cpu-time used.
  5485.  
  5486.  
  5487. pprrooffiilleerr((_-_O_l_d_, _+_N_e_w))
  5488.     Query  or change  the status of  the profiler.   The  status is  one
  5489.     of  off,  plain or  cumulative.    plain implies  the  time used  by
  5490.     children  of a predicate is not added to the time of  the predicate.
  5491.     For  status cumulative  the time  of children is  added (except  for
  5492.     recursive  calls).     Cumulative profiling  implies  the  stack  is
  5493.     scanned  up  to the  top  on  each time  slice  to find  all  active
  5494.     predicates.    This implies the  overhead grows  with the number  of
  5495.     active  frames on the stack.  Cumulative profiling  starts debugging
  5496.     mode  to disable tail recursion optimisation, which  would otherwise
  5497.     remove  the necessary  parent environments.   Switching status  from
  5498.     plain  to cumulative  resets the profiler.    Switching to and  from
  5499.     status  off does not reset  the collected statistics, thus  allowing
  5500.     to suspend profiling for certain parts of the program.
  5501.  
  5502.  
  5503. rreesseett__pprrooffiilleerr
  5504.     Switches the profiler to off and clears all collected statistics.
  5505.  
  5506.  
  5507. pprrooffiillee__ccoouunntt((_+_H_e_a_d_, _-_C_a_l_l_s_, _-_P_r_o_m_i_l_a_g_e))
  5508.     Obtain profile statistics  of the predicate specified by _H_e_a_d.  _H_e_a_d
  5509.     is  an atom  for predicates with  arity 0  or a term  with the  same
  5510.     name  and arity as the predicate required (see ccuurrrreenntt__pprreeddiiccaattee//22).
  5511.     _C_a_l_l_s  is unified with the number  of `calls' and `redos' while  the
  5512.     profiler was active.   _P_r_o_m_i_l_a_g_e is unified with the relative number
  5513.     of  counts the predicate  was active (cumulative)  or on top of  the
  5514.     stack (plain).  _P_r_o_m_i_l_a_g_e is an integer between 0 and 1000.
  5515.  
  5516.  
  5517. 33..4400 MMeemmoorryy MMaannaaggeemmeenntt
  5518.  
  5519. Note:   lliimmiitt__ssttaacckk//22and  ttrriimm__ssttaacckkss//00have no  effect on machines  that
  5520. do  not  offer  dynamic  stack expansion.     On  these  machines  these
  5521. predicates simply succeed to improve portability.
  5522.  
  5523.  
  5524. ggaarrbbaaggee__ccoolllleecctt
  5525.     Invoke  the global-  and trail  stack garbage collector.    Normally
  5526.     the  garbage   collector  is  invoked  automatically  if  necessary.
  5527.     Explicit  invocation  might   be  useful  to  reduce  the  need  for
  5528.     garbage  collections in time critical segments  of the code.   After
  5529.     the  garbage  collection  ttrriimm__ssttaacckkss//00 is invoked  to  release  the
  5530.     collected memory resources.
  5531.  
  5532.  
  5533. lliimmiitt__ssttaacckk((_+_K_e_y_, _+_K_b_y_t_e_s))
  5534.     Limit one of the  stack areas to the specified value.  _K_e_y is one of
  5535.     local,  global or trail.   The limit  is an integer, expressing  the
  5536.     desired  stack limit in K  bytes.  If  the desired limit is  smaller
  5537.     than  the currently  used value,  the limit  is set  to the  nearest
  5538.     legal  value above the currently used  value.  If the desired  value
  5539.     is  larger than the maximum, the maximum is taken.  Finally,  if the
  5540.     desired value is either  0 or the atom unlimited the limit is set to
  5541.     its  maximum.   The maximum and initial  limit is determined by  the
  5542.     command line options -L, -G and -T.
  5543.  
  5544.  
  5545. ttrriimm__ssttaacckkss
  5546.     Release  stack memory resources that are not in use at  this moment,
  5547.     returning them to the  operating system.  Trim stack is a relatively
  5548.     cheap  call.    It can  be used  to release  memory  resources in  a
  5549.     backtracking  loop, where the  iterations require typically  seconds
  5550.     of execution time  and very different, potentially large, amounts of
  5551.     stack space.  Such a loop should be written as follows:
  5552.  
  5553.     loop :-
  5554.             generator,
  5555.                 trim_stacks,
  5556.                 potentially_expensive_operation,
  5557.             stop_condition, !.
  5558.  
  5559.     The  prolog top level  loop is written  this way, reclaiming  memory
  5560.     resources after every user query.
  5561.  
  5562.  
  5563. ssttaacckk__ppaarraammeetteerr((_+_S_t_a_c_k_, _+_K_e_y_, _-_O_l_d_, _+_N_e_w))
  5564.     Query/set  a parameter  for the  runtime stacks.   _S_t_a_c_k  is one  of
  5565.     local,  global, trail or  argument.   The table below describes  the
  5566.     _K_e_y/_V_a_l_u_e pairs.  Old is first unified with the current value.
  5567.  
  5568.         ____________________________________________________________
  5569.         | limit    |Maximum size of the stack in bytes               |
  5570.         |_min_free_|Minimum_free_space_at_entry_of_foreign_predicate_|
  5571.  
  5572.     This predicate is  currently only available on versions that use the
  5573.     stack-shifter  to enlarge the runtime  stacks when necessary.   It's
  5574.     definition is subject to change.
  5575.  
  5576.  
  5577. 33..4411 WWiinnddoowwss DDDDEE iinntteerrffaaccee
  5578.  
  5579. The  predicates in  this  section  deal with  MS-Windows  `Dynamic  Data
  5580. Exchange'  or DDE  protocol.    A Windows  DDE  conversation is  a  form
  5581. of interprocess  communication based  on sending reserved  window-events
  5582. between the communicating processes.
  5583.  
  5584. See also section 5.4 for loading Windows DLL's into SWI-Prolog.
  5585.  
  5586.  
  5587. 33..4411..11 DDDDEE cclliieenntt iinntteerrffaaccee
  5588.  
  5589. The DDE client interface  allows Prolog to talk to DDE  server programs.
  5590. We  will demonstrate  the use  of the  DDE interface  using the  Windows
  5591. PROGMAN (Program Manager) application:
  5592.  
  5593. 1 ?- open_dde_conversation(progman, progman, C).
  5594.  
  5595. C = 0
  5596. 2 ?- dde_request(0, groups, X)
  5597.  
  5598. --> Unifies X with description of groups
  5599.  
  5600. 3 ?- dde_execute(0, '[CreateGroup("DDE Demo")]').
  5601.  
  5602. Yes
  5603.  
  5604. 4 ?- close_dde_conversation(0).
  5605.  
  5606. Yes
  5607.  
  5608. For  details   on  interacting  with   progman,   use  the  SDK   online
  5609. manual  section on  the  Shell  DDE interface.     See also  the  Prolog
  5610. library(progman),  which  may be  used  to  write simple  Windows  setup
  5611. scripts in Prolog.
  5612.  
  5613.  
  5614. ooppeenn__ddddee__ccoonnvveerrssaattiioonn((_+_S_e_r_v_i_c_e_, _+_T_o_p_i_c_, _-_H_a_n_d_l_e))
  5615.     Open a conversation  with a server supporting the given service name
  5616.     and  topic (atoms).    If successful,  _H_a_n_d_l_e  may be  used to  send
  5617.     transactions  to the server.    If no willing  server is found  this
  5618.     predicate fails silently.
  5619.  
  5620.  
  5621. cclloossee__ddddee__ccoonnvveerrssaattiioonn((_+_H_a_n_d_l_e))
  5622.     Close  the  conversation   associated  with  _H_a_n_d_l_e.     All  opened
  5623.     conversations  should  be  closed  when they're  no  longer  needed,
  5624.     although  the system  will  close any  that remain  open on  process
  5625.     termination.
  5626.  
  5627.  
  5628. ddddee__rreeqquueesstt((_+_H_a_n_d_l_e_, _+_I_t_e_m_, _-_V_a_l_u_e))
  5629.     Request  a value from the server.   _I_t_e_m is an atom  that identifies
  5630.     the  requested  data,  and  _V_a_l_u_e will  be  a string  (CF_TEXT  data
  5631.     in  DDE  parlance)  representing  that  data,   if  the  request  is
  5632.     successful.    If unsuccessful, _V_a_l_u_e  will be  unified with a  term
  5633.     of  form error(<_R_e_a_s_o_n>),  identifying the problem.   This call  uses
  5634.     SWI-Prolog  string objects  to return  the value  rather then  atoms
  5635.     to  reduce the  load on  the atom-space.    See section  3.20 for  a
  5636.     discussion on this data type.
  5637.  
  5638.  
  5639. ddddee__eexxeeccuuttee((_+_H_a_n_d_l_e_, _+_C_o_m_m_a_n_d))
  5640.     Request  the  DDE   server  to  execute  the  given  command-string.
  5641.     Succeeds  if the  command  could be  executed and  fails with  error
  5642.     message otherwise.
  5643.  
  5644.  
  5645. ddddee__ppookkee((_+_H_a_n_d_l_e_, _+_I_t_e_m_, _+_C_o_m_m_a_n_d))
  5646.     Issue  a POKE command to the server on the specified _I_t_e_m.   Command
  5647.     is passed as data of type CF_TEXT.
  5648.  
  5649.  
  5650. 33..4411..22 DDDDEE sseerrvveerr mmooddee
  5651.  
  5652. The (autoload)  library(dde) defines  primitives to  realise simple  DDE
  5653. server applications in  SWI-Prolog.  These  features are provided as  of
  5654. version 2.0.6 and should be regarded prototypes.  The  C-part of the DDE
  5655. server can  handle some  more primitives,  so if you  need features  not
  5656. provided by this interface, please study library(dde).
  5657.  
  5658.  
  5659. ddddee__rreeggiisstteerr__sseerrvviiccee((_+_T_e_m_p_l_a_t_e_, _+_G_o_a_l))
  5660.     Register  a server  to handle  DDE request or  DDE execute  requests
  5661.     from  other applications.  To register a service for a  DDE request,
  5662.     _T_e_m_p_l_a_t_e is of the form:
  5663.  
  5664.          +Service(+Topic, +Item, +Value)
  5665.  
  5666.     _S_e_r_v_i_c_e  is the name  of the DDE  service provided (like progman  in
  5667.     the  client example  above).   _T_o_p_i_c is either  an atom,  indicating
  5668.     _G_o_a_l  only handles requests  on this topic  or a variable that  also
  5669.     appears  in _G_o_a_l.  _I_t_e_m and _V_a_l_u_e are variables that also  appear in
  5670.     _G_o_a_l.
  5671.  
  5672.     The  example below  registers the Prolog  ffeeaattuurree//22 predicate to  be
  5673.     accessible  from other applications.  The request may be  given from
  5674.     the same Prolog as well as from another application.
  5675.  
  5676.     ?- dde_register_service(prolog(feature, F, V),
  5677.                             feature(F, V)).
  5678.  
  5679.     ?- open_dde_conversation(prolog, feature, Handle),
  5680.        dde_request(Handle, home, Home),
  5681.        close_dde_conversation(Handle).
  5682.  
  5683.     Home = '/usr/local/lib/pl-2.0.6/'
  5684.  
  5685.     Handling  DDE execute requests  is very similar.   In this case  the
  5686.     template is of the form:
  5687.  
  5688.          +Service(+Topic, +Item)
  5689.  
  5690.     Passing  a _V_a_l_u_e argument is  not needed as execute requests  either
  5691.     succeed  or fail.  If _G_o_a_l  fails, a `not processed' is  passed back
  5692.     to the caller of the DDE request.
  5693.  
  5694.  
  5695. ddddee__uunnrreeggiisstteerr__sseerrvviiccee((_+_S_e_r_v_i_c_e))
  5696.     Stop  responding  to  _S_e_r_v_i_c_e.     If  Prolog  is  halted,  it  will
  5697.     automatically call this on all open services.
  5698.  
  5699.  
  5700. ddddee__ccuurrrreenntt__sseerrvviiccee((_-_S_e_r_v_i_c_e_, _-_T_o_p_i_c))
  5701.     Find currently registered services and the topics served on them.
  5702.  
  5703.  
  5704. ddddee__ccuurrrreenntt__ccoonnnneeccttiioonn((_-_S_e_r_v_i_c_e_, _-_T_o_p_i_c))
  5705.     Find currently open conversations.
  5706.  
  5707.  
  5708. 33..4422 MMiisscceellllaanneeoouuss
  5709.  
  5710.  
  5711. ddwwiimm__mmaattcchh((_+_A_t_o_m_1_, _+_A_t_o_m_2))
  5712.     Succeeds  if _A_t_o_m_1 matches _A_t_o_m_2  in `Do What I  Mean' sense.   Both
  5713.     _A_t_o_m_1  and _A_t_o_m_2  may also be  integers or  floats.   The two  atoms
  5714.     match if:
  5715.  
  5716.       o  They are identical
  5717.  
  5718.       o  They differ by one character (spy  spu)
  5719.  
  5720.       o  One character is inserted/deleted (debug  deug)
  5721.  
  5722.       o  Two characters are transposed (trace  tarce)
  5723.  
  5724.       o  `Sub-words' are glued  differently (existsfile   existsFile
  5725.          exists_file)
  5726.  
  5727.       o  Two   adjacent  sub   words  are   transposed   (existsFile
  5728.          fileExists)
  5729.  
  5730.  
  5731. ddwwiimm__mmaattcchh((_+_A_t_o_m_1_, _+_A_t_o_m_2_, _-_D_i_f_f_e_r_e_n_c_e))
  5732.     Equivalent  to  ddwwiimm__mmaattcchh//22,  but unifies  _D_i_f_f_e_r_e_n_c_e with  an  atom
  5733.     identifying the the  difference between _A_t_o_m_1 and _A_t_o_m_2.  The return
  5734.     values  are (in the same  order as above):   equal, mismatched_char,
  5735.     inserted_char, transposed_char, separated and transposed_word.
  5736.  
  5737.  
  5738. wwiillddccaarrdd__mmaattcchh((_+_P_a_t_t_e_r_n_, _+_S_t_r_i_n_g))
  5739.     Succeeds  if _S_t_r_i_n_g matches the  wildcard pattern _P_a_t_t_e_r_n.   _P_a_t_t_e_r_n
  5740.     is very similar the  the Unix csh pattern matcher.  The patterns are
  5741.     given below:
  5742.  
  5743.      ?      Matches one arbitrary character.
  5744.  
  5745.      *      Matches any number of arbitrary characters.
  5746.      [...]  Matches one of the characters specified between the brackets.  <_c_h_a_r_1>-<_c_h_a_r_2>indicates a range.
  5747.      {...}  Matches any of the patterns of the comma separated list between the braces.
  5748.  
  5749.     Example:
  5750.  
  5751.     ?- wildcard_match('[a-z]*.{pro,pl}[%~]', 'a_hello.pl%').
  5752.  
  5753.     Yes.
  5754.  
  5755.  
  5756. ggeennssyymm((_+_B_a_s_e_, _-_U_n_i_q_u_e))
  5757.     Generate  a unique  atom from base  _B_a_s_e and  unify it with  _U_n_i_q_u_e.
  5758.     _B_a_s_e  should be an  atom.  The  first call will  return <_b_a_s_e>1,  the
  5759.     next  <_b_a_s_e>2, etc.   Note that this is  no warrant that the atom  is
  5760.     unique in the system.
  5761.  
  5762.  
  5763. sslleeeepp((_+_T_i_m_e))
  5764.     Suspend  execution _T_i_m_e seconds.   _T_i_m_e  is either a floating  point
  5765.     number  or an  integer.   Granularity is  dependent on the  system's
  5766.     timer  granularity.   A  negative time  causes the  timer to  return
  5767.     immediately.   On  most non-realtime operating  systems we can  only
  5768.     ensure execution is suspended for aatt lleeaasstt _T_i_m_e seconds.
  5769.  
  5770.  
  5771. CChhaapptteerr 44..  UUSSIINNGG MMOODDUULLEESS
  5772.  
  5773.  
  5774. 44..11 WWhhyy UUssiinngg MMoodduulleess??
  5775.  
  5776. In  traditional Prolog  systems the  predicate  space was  flat.    This
  5777. approach   is  not   very  suitable   for  the   development  of   large
  5778. applications, certainly not if these applications are  developed by more
  5779. than  one programmer.     In many  cases,  the  definition of  a  Prolog
  5780. predicate requires  sub-predicates that  are intended  only to  complete
  5781. the definition of the main predicate.  With a  flat and global predicate
  5782. space these support predicates will be visible from the entire program.
  5783.  
  5784. For this reason,  it is desirable that  each source module has it's  own
  5785. predicate space.    A module consists  of a  declaration for it's  name,
  5786. it's _p_u_b_l_i_c  _p_r_e_d_i_c_a_t_e_s and the  predicates themselves.   This  approach
  5787. allow the programmer  to use short (local) names for  support predicates
  5788. without worrying  about name  conflicts with the  support predicates  of
  5789. other  modules.    The  module  declaration also  makes  explicit  which
  5790. predicates are  meant for public usage  and which for private  purposes.
  5791. Finally,  using the  module information,  cross  reference programs  can
  5792. indicate possible problems much better.
  5793.  
  5794.  
  5795. 44..22 NNaammee--bbaasseedd vveerrssuuss PPrreeddiiccaattee--bbaasseedd MMoodduulleess
  5796.  
  5797. Two approaches to  realize a module system  are commonly used in  Prolog
  5798. and other  languages.  The  first one is  the _n_a_m_e _b_a_s_e_d module  system.
  5799. In these  systems, each  atom read  is tagged  (normally prefixed)  with
  5800. the module  name, with  the exception  of those atoms  that are  defined
  5801. _p_u_b_l_i_c.   In the  second approach, each  module actually implements  its
  5802. own predicate space.
  5803.  
  5804. A  critical  problem with  using  modules  in Prolog  is  introduced  by
  5805. the  meta-predicates  that  transform between  Prolog  data  and  Prolog
  5806. predicates.  Consider the case where we write:
  5807.  
  5808. :- module(extend, [add_extension/3]).
  5809.  
  5810. add_extension(Extension, Plain, Extended) :-
  5811.         maplist(extend_atom(Extension), Plain, Extended).
  5812.  
  5813. extend_atom(Extension, Plain, Extended) :-
  5814.         concat(Plain, Extension, Extended).
  5815.  
  5816. In this case  we would like maplist to  call extend_atom/3 in the  module
  5817. extend.   A name based module  system will do this  correctly.  It  will
  5818. tag the atom  extend_atom  with the module and  maplist will use this  to
  5819. construct the tagged  term extend_atom/3.   A name based module  however,
  5820. will not only tag the  atoms that will eventually be used to refer  to a
  5821. predicate, but aallll atoms that are not declared public.   So, with a name
  5822. based module system also  data is local to the module.   This introduces
  5823. another serious problem:
  5824.  
  5825. :- module(action, [action/3]).
  5826.  
  5827. action(Object, sleep, Arg) :- ....
  5828. action(Object, awake, Arg) :- ....
  5829.  
  5830. :- module(process, [awake_process/2]).
  5831.  
  5832. awake_process(Process, Arg) :-
  5833.         action(Process, awake, Arg).
  5834.  
  5835. This code  uses a simple  object-oriented implementation technique  were
  5836. atoms are used as  method selectors.  Using a name based  module system,
  5837. this code will  not work, unless we  declare the selectors public  atoms
  5838. in all  modules that use them.   Predicate  based module systems do  not
  5839. require particular precautions for handling this case.
  5840.  
  5841. It  appears  we  have to  choose  either  to  have  local  data,  or  to
  5842. have  trouble with  meta-predicates.    Probably it  is best  to  choose
  5843. for  the  predicate  based approach  as  novice  users  will  not  often
  5844. write  generic meta-predicates  that  have to  be used  across  multiple
  5845. modules, but are likely  to write programs that pass data  around across
  5846. modules.   Experienced Prolog  programmers should be  able to deal  with
  5847. the complexities of meta-predicates in a predicate based module system.
  5848.  
  5849.  
  5850. 44..33 DDeeffiinniinngg aa MMoodduullee
  5851.  
  5852. Modules normally are  created by loading a _m_o_d_u_l_e  _f_i_l_e.  A module  file
  5853. is a file holding a mmoodduullee//22 directive as its first term.   The mmoodduullee//22
  5854. directive declares  the name  and the public  (i.e. externally  visible)
  5855. predicates of  the module.   The  rest of  the file is  loaded into  the
  5856. module.  Below is an example of a module file, defining rreevveerrssee//22.
  5857.  
  5858. :- module(reverse, [reverse/2]).
  5859.  
  5860. reverse(List1, List2) :-
  5861.         rev(List1, [], List2).
  5862.  
  5863. rev([], List, List).
  5864. rev([Head|List1], List2, List3) :-
  5865.         rev(List1, [Head|List2], List3).
  5866.  
  5867.  
  5868. 44..44 IImmppoorrttiinngg PPrreeddiiccaatteess iinnttoo aa MMoodduullee
  5869.  
  5870. As  explained  before,  in  the  predicate  based  approach  adapted  by
  5871. SWI-Prolog, each  module has it's own predicate  space.  In  SWI-Prolog,
  5872. a module  initially is  completely empty.   Predicates  can be added  to
  5873. a  module by  loading a  module  file as  demonstrated in  the  previous
  5874. section, using assert or by _i_m_p_o_r_t_i_n_g them from another module.
  5875.  
  5876. Two mechanisms for  importing predicates explicitly from another  module
  5877. exist.   The uussee__mmoodduullee//[[11,,22]] predicates load a  module file and  import
  5878. (part of  the) public predicates  of the file.   The iimmppoorrtt//11  predicate
  5879. imports any predicate from any module.
  5880.  
  5881.  
  5882. uussee__mmoodduullee((_+_F_i_l_e))
  5883.     Load  the  file(s) specified  with _F_i_l_e  just  like eennssuurree__llooaaddeedd//11.
  5884.     The  files should  all be  module files.    All exported  predicates
  5885.     from  the  loaded  files  are  imported  into  the  context  module.
  5886.     The  difference between  this predicate and  eennssuurree__llooaaddeedd//11 becomes
  5887.     apparent  if the file  is already  loaded into another  module.   In
  5888.     this  case eennssuurree__llooaaddeedd//11 does nothing; use_module will import  all
  5889.     public predicates of the module into the current context module.
  5890.  
  5891.  
  5892. uussee__mmoodduullee((_+_F_i_l_e_, _+_I_m_p_o_r_t_L_i_s_t))
  5893.     Load  the file  specified  with _F_i_l_e  (only one  file is  accepted).
  5894.     _F_i_l_e  should be a module file.   _I_m_p_o_r_t_L_i_s_t is a list  of name/arity
  5895.     pairs  specifying the  predicates that should  be imported from  the
  5896.     loaded  module.   If a predicate is  specified that is not  exported
  5897.     from  the loaded module  a warning will be  printed.  The  predicate
  5898.     will nevertheless be imported to simplify debugging.
  5899.  
  5900.  
  5901. iimmppoorrtt((_+_H_e_a_d))
  5902.     Import predicate _H_e_a_d  into the current context module.  _H_e_a_d should
  5903.     specify the  source module using the <_m_o_d_u_l_e>:<_t_e_r_m>construct.  Note
  5904.     that  predicates are normally imported  using one of the  directives
  5905.     uussee__mmoodduullee//[[11,,22]].    iimmppoorrtt//11 is  meant  for handling  imports  into
  5906.     dynamically created modules.
  5907.  
  5908. It  would  be rather  inconvenient  to  have to  import  each  predicate
  5909. referred to by  the module, including the  system predicates.  For  this
  5910. reason each  module is  assigned a _d_e_f_a_u_l_t  _m_o_d_u_l_e.   All predicates  in
  5911. the default  module are  available without  extra declarations.    Their
  5912. definition however can be overruled in the local module.   This schedule
  5913. is implemented  by the exception handling  mechanism of SWI-Prolog:   if
  5914. an  undefined predicate  exception is  raised for  a  predicate in  some
  5915. module, the exception  handler first tries to import the  predicate from
  5916. the module's default module.  On success, normal execution is resumed.
  5917.  
  5918.  
  5919. 44..44..11 RReesseerrvveedd MMoodduulleess
  5920.  
  5921. SWI-Prolog contains two  special modules.   The first one is the  module
  5922. system.    This module  contains all  built-in  predicates described  in
  5923. this  manual.   Module  system has  no default  module  assigned to  it.
  5924. The second  special module is the  module user.   This module forms  the
  5925. initial  working space  of the  user.    Initially  it is  empty.    The
  5926. default module of module  user is system, making all  built-in predicate
  5927. definitions available  as defaults.    Built-in predicates  thus can  be
  5928. overruled by defining them in module user before they are used.
  5929.  
  5930. All other  modules default to module  user.   This implies they can  use
  5931. all predicates imported into user without explicitly importing them.
  5932.  
  5933.  
  5934. 44..55 UUssiinngg tthhee MMoodduullee SSyysstteemm
  5935.  
  5936. The  current structure  of  the module  system  has been  designed  with
  5937. some specific  organisations for  large programs  in mind.   Many  large
  5938. programs  define a  basic  library layer  on  top  of which  the  actual
  5939. program itself  is defined.    The module  user, acting  as the  default
  5940. module for all  other modules of the  program can be used to  distribute
  5941. these definitions over  all program module without introducing the  need
  5942. to  import this  common layer  each time  explicitly.   It  can also  be
  5943. used to  redefine built-in predicates  if this  is required to  maintain
  5944. compatibility  to some  other Prolog  implementation.    Typically,  the
  5945. loadfile of a large application looks like this:
  5946.  
  5947. :- use_module(compatibility).   % load XYZ prolog compatibility
  5948.  
  5949. :- use_module(                  % load generic parts
  5950.         [ error                 % errors and warnings
  5951.         , goodies               % general goodies (library extensions)
  5952.         , debug                 % application specific debugging
  5953.         , virtual_machine       % virtual machine of application
  5954.         , ...                   % more generic stuff
  5955.         ]).
  5956.  
  5957. :- ensure_loaded(
  5958.         [ ...                   % the application itself
  5959.         ]).
  5960.  
  5961. The  `use_module' declarations  will  import the  public predicates  from
  5962. the  generic  modules  into  the  user  module.     The  `ensure_loaded'
  5963. directive  loads the  modules that  constitute  the actual  application.
  5964. It is  assumed these  modules import  predicates from  each other  using
  5965. uussee__mmoodduullee//[[11,,22]]as far as necessary.
  5966.  
  5967. In combination  with the  object-oriented schema described  below it  is
  5968. possible  to define  a neat  modular  architecture.   The  generic  code
  5969. defines general utilities  and the message passing predicates  (invoke/3
  5970. in the  example below).    The application modules  define classes  that
  5971. communicate using the message passing predicates.
  5972.  
  5973.  
  5974. 44..55..11 OObbjjeecctt OOrriieenntteedd PPrrooggrraammmmiinngg
  5975.  
  5976. Another typical  way to use  the module system  is for defining  classes
  5977. within  an object  oriented  paradigm.    The  class structure  and  the
  5978. methods  of  a class  can  be  defined  in a  module  and  the  explicit
  5979. module-boundary overruling  describes in  section 4.6.2 can  by used  by
  5980. the message passing  code to invoke the behaviour.   An outline of  this
  5981. mechanism is given below.
  5982.  
  5983. %       Define class point
  5984.  
  5985. :- module(point, []).           % class point, no exports
  5986.  
  5987. %        name           type,           default access
  5988. %                                       value
  5989.  
  5990. variable(x,             integer,        0,      both).
  5991. variable(y,             integer,        0,      both).
  5992.  
  5993. %         method name   predicate name  arguments
  5994.  
  5995. behaviour(mirror,       mirror,         []).
  5996.  
  5997. mirror(P) :-
  5998.         fetch(P, x, X),
  5999.         fetch(P, y, Y),
  6000.         store(P, y, X),
  6001.         store(P, x, Y).
  6002.  
  6003. The predicates fetch/3  and store/3 are predicates that change  instance
  6004. variables of instances.  The figure below indicates  how message passing
  6005. can easily be implemented:
  6006.  
  6007. %       invoke(+Instance, +Selector, ?ArgumentList)
  6008. %       send a message to an instance
  6009.  
  6010. invoke(I, S, Args) :-
  6011.         class_of_instance(I, Class),
  6012.         Class:behaviour(S, P, ArgCheck), !,
  6013.         convert_arguments(ArgCheck, Args, ConvArgs),
  6014.         Goal =.. [P|ConvArgs],
  6015.         Class:Goal.
  6016.  
  6017. The construct  <_M_o_d_u_l_e>:<_G_o_a_l> explicitly  calls _G_o_a_l  in module  _M_o_d_u_l_e.
  6018. It is discussed in more detail in section 3.7.
  6019.  
  6020.  
  6021. 44..66 MMeettaa--PPrreeddiiccaatteess iinn MMoodduulleess
  6022.  
  6023. As indicated  in the introduction,  the problem  with a predicate  based
  6024. module system lies in the difficulty to find the  correct predicate from
  6025. a Prolog  term.   The predicate `solution(Solution)'  can exist in  more
  6026. than one  module, but `assert(solution(4))'  in some module is  supposed
  6027. to refer to the correct version of solution/1.
  6028.  
  6029. Various  approaches are  possible to  solve this  problem.    One is  to
  6030. add an extra  argument to all predicates (e.g. `assert(Module,  Term)').
  6031. Another is to tag  the term somehow to indicate which module  is desired
  6032. (e.g. `assert(Module:Term)').   Both approaches are not very  attractive
  6033. as  they make  the user  responsible for  choosing  the correct  module,
  6034. inviting  unclear  programming by  asserting  in  other modules.     The
  6035. predicate aasssseerrtt//11  is supposed  to assert  in the module  it is  called
  6036. from and should do  so without being told explicitly.  For  this reason,
  6037. the notion _c_o_n_t_e_x_t _m_o_d_u_l_e has been introduced.
  6038.  
  6039.  
  6040. 44..66..11 DDeeffiinniittiioonn aanndd CCoonntteexxtt MMoodduullee
  6041.  
  6042. Each  predicate  of  the program  is  assigned  a  module,  called  it's
  6043. _d_e_f_i_n_i_t_i_o_n _m_o_d_u_l_e.   The definition module of a predicate is  always the
  6044. module in which the predicate was originally defined.   Each active goal
  6045. in the Prolog system has a _c_o_n_t_e_x_t _m_o_d_u_l_e assigned to it.
  6046.  
  6047. The  context module  is used  to  find predicates  from a  Prolog  term.
  6048. By  default, this  module  is the  definition  module of  the  predicate
  6049. running the  goal.   For meta-predicates  however, this  is the  context
  6050. module of the  goal that invoked them.   We call this _m_o_d_u_l_e___t_r_a_n_s_p_a_r_e_n_t
  6051. in SWI-Prolog.    In the `using  maplist' example  above, the  predicate
  6052. mmaapplliisstt//33  is declared  module_transparent.    This  implies the  context
  6053. module remains extend, the  context module of add_extension/3.   This way
  6054. mmaapplliisstt//33 can decide to call extend_atom in module extend rather than in
  6055. it's own definition module.
  6056.  
  6057. All built-in predicates that  refer to predicates via a Prolog  term are
  6058. declared module_transparent.  Below is the code defining maplist.
  6059.  
  6060. :- module(maplist, maplist/3).
  6061.  
  6062. :- module_transparent maplist/3.
  6063.  
  6064. %       maplist(+Goal, +List1, ?List2)
  6065. %       True if Goal can successfully be applied to all succes-
  6066. sive pairs
  6067. %       of elements of List1 and List2.
  6068.  
  6069. maplist(_, [], []).
  6070. maplist(Goal, [Elem1|Tail1], [Elem2|Tail2]) :-
  6071.         apply(Goal, [Elem1, Elem2]),
  6072.         maplist(Goal, Tail1, Tail2).
  6073.  
  6074.  
  6075. 44..66..22 OOvveerrrruulliinngg MMoodduullee BBoouunnddaarriieess
  6076.  
  6077. The  mechanism  above  is sufficient  to  create  an  acceptable  module
  6078. system.   There are however cases in which  we would like to be  able to
  6079. overrule this  schema and  explicitly call  a predicate  in some  module
  6080. or assert  explicitly in some  module.   The first  is useful to  invoke
  6081. goals  in  some module  from  the  user's  toplevel or  to  implement  a
  6082. object-oriented system (see above).  The latter is  useful to create and
  6083. modify _d_y_n_a_m_i_c _m_o_d_u_l_e_s (see section 4.7).
  6084.  
  6085. For  this purpose,  the reserved  term ://22  has  been introduced.    All
  6086. built-in predicates  that transform  a term into  a predicate  reference
  6087. will check whether  this term is of  the form `<_M_o_d_u_l_e>:<_T_e_r_m>'.  If  so,
  6088. the predicate is  searched for in _M_o_d_u_l_e  instead of the goal's  context
  6089. module.   The :  operator may be  nested, in  which case the  inner-most
  6090. module is used.
  6091.  
  6092. The special  calling construct  <_M_o_d_u_l_e>:<_G_o_a_l>pretends  _G_o_a_l is  called
  6093. from _M_o_d_u_l_e instead of the context module.  Examples:
  6094.  
  6095. ?- assert(world:done).  % asserts done/0 into module world
  6096. ?- world:assert(done).  % the same
  6097. ?- world:done.          % calls done/0 in module world
  6098.  
  6099.  
  6100. 44..77 DDyynnaammiicc MMoodduulleess
  6101.  
  6102. So  far,   we  discussed  modules  that   were  created  by  loading   a
  6103. module-file.     These  modules  have  been   introduced  on  facilitate
  6104. the  development  of  large  applications.     The   modules  are  fully
  6105. defined at  load-time of the  application and  normally will not  change
  6106. during  execution.   Having  the  notion of  a set  of  predicates as  a
  6107. self-contained world can be attractive for other purposes as  well.  For
  6108. example, assume  an application that  can reason about multiple  worlds.
  6109. It is attractive  to store the data of  a particular world in a  module,
  6110. so we extract information from a world simply by  invoking goals in this
  6111. world.
  6112.  
  6113. Dynamic modules  can easily  be created.   Any  built-in predicate  that
  6114. tries  to locate  a predicate  in  a specific  module will  create  this
  6115. module as a side-effect if it did not yet exist.  Example:
  6116.  
  6117. ?- assert(world_a:consistent),
  6118.    world_a:unknown(_, fail).
  6119.  
  6120. These  calls  create  a  module  called  `world_a'  and  make  the  call
  6121. `world_a:consistent'  succeed.  Undefined  predicates will not start  the
  6122. tracer or autoloader for this module (see uunnkknnoowwnn//22).
  6123.  
  6124. Import and  export from dynamically  created world  is arranged via  the
  6125. predicates iimmppoorrtt//11 and eexxppoorrtt//11:
  6126.  
  6127. ?- world_b:export(solve(_,_)).          % exports solve/2 from world_b
  6128. ?- world_c:import(world_b:solve(_,_)).  % and import it to world_c
  6129.  
  6130.  
  6131. 44..88 MMoodduullee HHaannddlliinngg PPrreeddiiccaatteess
  6132.  
  6133. This section gives the predicate definitions for  the remaining built-in
  6134. predicates that handle modules.
  6135.  
  6136.  
  6137. ::-- mmoodduullee((_+_M_o_d_u_l_e_, _+_P_u_b_l_i_c_L_i_s_t))
  6138.     This directive can only  be used as the first term of a source file.
  6139.     It  declares  the file  to be  a _m_o_d_u_l_e  _f_i_l_e,  defining _M_o_d_u_l_e  and
  6140.     exporting  the predicates of  _P_u_b_l_i_c_L_i_s_t.   _P_u_b_l_i_c_L_i_s_t is a list  of
  6141.     name/arity pairs.
  6142.  
  6143.  
  6144. mmoodduullee__ttrraannssppaarreenntt _+_P_r_e_d_s
  6145.     _P_r_e_d_s   is  a  comma  separated  list  of  name/arity   pairs  (like
  6146.     ddyynnaammiicc//11).     Each goal  associated  with a  transparent  declared
  6147.     predicate will inherit the _c_o_n_t_e_x_t _m_o_d_u_l_e from its parent goal.
  6148.  
  6149.  
  6150. mmeettaa__pprreeddiiccaattee _+_H_e_a_d_s
  6151.     This  predicate  is  defined  in  library(quintus)  and  provides  a
  6152.     partial  emulation of the Quintus predicate.  See section  4.9.1 for
  6153.     details.
  6154.  
  6155.  
  6156. ccuurrrreenntt__mmoodduullee((_-_M_o_d_u_l_e))
  6157.     Generates all currently known modules.
  6158.  
  6159.  
  6160. ccuurrrreenntt__mmoodduullee((_?_M_o_d_u_l_e_, _?_F_i_l_e))
  6161.     Is  true if _F_i_l_e is the file from which _M_o_d_u_l_e was loaded.   _F_i_l_e is
  6162.     the internal canonical filename.  See also ssoouurrccee__ffiillee//[[11,,22]].
  6163.  
  6164.  
  6165. ccoonntteexxtt__mmoodduullee((_-_M_o_d_u_l_e))
  6166.     Unify   _M_o_d_u_l_e  with  the  context  module  of  the   current  goal.
  6167.     ccoonntteexxtt__mmoodduullee//11 itself is transparent.
  6168.  
  6169.  
  6170. eexxppoorrtt((_+_H_e_a_d))
  6171.     Add  a predicate to  the public list  of the context  module.   This
  6172.     implies  the predicate will be imported into another module  if this
  6173.     module  is imported with uussee__mmoodduullee//[[11,,22]].  Note  that predicates are
  6174.     normally  exported using the directive mmoodduullee//22.  eexxppoorrtt//11  is meant
  6175.     to handle export from dynamically created modules.
  6176.  
  6177.  
  6178. eexxppoorrtt__lliisstt((_+_M_o_d_u_l_e_, _?_E_x_p_o_r_t_s))
  6179.     Unifies  _E_x_p_o_r_t_s with a list of terms.   Each term has the  name and
  6180.     arity  of a public predicate of _M_o_d_u_l_e.   The order of the  terms in
  6181.     _E_x_p_o_r_t_s is not defined.  See also pprreeddiiccaattee__pprrooppeerrttyy//22.
  6182.  
  6183.  
  6184. ddeeffaauulltt__mmoodduullee((_+_M_o_d_u_l_e_, _-_D_e_f_a_u_l_t))
  6185.     Succesively unifies _D_e_f_a_u_l_t  with the module names from which a call
  6186.     in  _M_o_d_u_l_e attempts to  use the  definition.   For the module  user,
  6187.     this  will generate user  and system.   For  any other module,  this
  6188.     will generate the module itself, followed by user and system.
  6189.  
  6190.  
  6191. mmoodduullee((_+_M_o_d_u_l_e))
  6192.     The  call module(Module) may be  used to switch the default  working
  6193.     module  for the interactive  toplevel (see pprroolloogg//00).   This may  be
  6194.     used  to when  debugging  a module.    The example  below lists  the
  6195.     clauses of file_of_label/2 in the module tex.
  6196.  
  6197.     1 ?- module(tex).
  6198.  
  6199.     Yes
  6200.     tex: 2 ?- listing(file_of_label/2).
  6201.     ...
  6202.  
  6203.  
  6204. 44..99 CCoommppaattiibbiilliittyy ooff tthhee MMoodduullee SSyysstteemm
  6205.  
  6206. The  principles behind  the  module system  of  SWI-Prolog differ  in  a
  6207. number of aspects from the Quintus Prolog module system.
  6208.  
  6209.   o The  SWI-Prolog module  system allows  the user  to redefine  system
  6210.     predicates.
  6211.  
  6212.   o All  predicates that are  available in the  system and user  modules
  6213.     are visible in all other modules as well.
  6214.  
  6215.   o Quintus  has the `mmeettaa__pprreeddiiccaattee//11' declaration were SWI-Prolog  has
  6216.     the mmoodduullee__ttrraannssppaarreenntt//11 declaration.
  6217.  
  6218. The mmeettaa__pprreeddiiccaattee//11 declaration  causes the compiler  to tag  arguments
  6219. that pass  module sensitive information  with the  module using the  ://22
  6220. operator.  This approach has some disadvantages:
  6221.  
  6222.   o Changing   a  meta_predicate   declaration  implies  all   predicates
  6223.     ccaalllliinngg  the predicate need to be reloaded.  This can  cause serious
  6224.     consistency problems.
  6225.  
  6226.   o It  does not help for dynamically defined predicates  calling module
  6227.     sensitive predicates.
  6228.  
  6229.   o It   slows  down   the  compiler   (at  least   in  the   SWI-Prolog
  6230.     architecture).
  6231.  
  6232.   o At  least within the  SWI-Prolog architecture the run-time  overhead
  6233.     is   larger  than  the   overhead  introduced  by  the   transparent
  6234.     mechanism.
  6235.  
  6236. Unfortunately  the   transparent  predicate   approach  also  has   some
  6237. disadvantages.   If a  predicate A passes  module sensitive information
  6238. to a  predicate B, passing  the same information to  a module sensitive
  6239. system predicate both  A and B  should be declared transparent.   Using
  6240. the Quintus approach only A  needs to be treated special (i.e. declared
  6241. with  mmeettaa__pprreeddiiccaattee//11).    A second  problem  arises if  the body  of  a
  6242. transparent  predicate uses  module sensitive  predicates  for which  it
  6243. wants to refer to its  own module.  Suppose we want to  define ffiinnddaallll//33
  6244. using aasssseerrtt//11  and rreettrraacctt//11.    The example  in figure  4.1 gives  the
  6245. solution.
  6246. :- module(findall, [findall/3]).
  6247.  
  6248. :- dynamic
  6249.  
  6250.         solution/1.
  6251.  
  6252. :- module_transparent
  6253.         findall/3,
  6254.         store/2.
  6255.  
  6256. findall(Var, Goal, Bag) :-
  6257.  
  6258.         assert(findall:solution('$mark')),
  6259.         store(Var, Goal),
  6260.         collect(Bag).
  6261.  
  6262. store(Var, Goal) :-
  6263.         Goal,                   % refers to context module of
  6264.                                 % caller of findall/3
  6265.         assert(findall:solution(Var)),
  6266.  
  6267.         fail.
  6268. store(_, _).
  6269.  
  6270. collect(Bag) :-
  6271.         ...,
  6272.  
  6273.                   Figure 4.1:  ffiinnddaallll//33 using modules
  6274.  
  6275.  
  6276. 44..99..11 EEmmuullaattiinngg mmeettaa__pprreeddiiccaattee//11
  6277.  
  6278. The Quintus  mmeettaa__pprreeddiiccaattee//11 directive can  in many  cases be  replaced
  6279. by  the   transparent  declaration.      Below  is  the  definition   of
  6280. mmeettaa__pprreeddiiccaattee//11as available from library(quintus).
  6281.  
  6282. :- op(1150, fx, (meta_predicate)).
  6283.  
  6284. meta_predicate((Head, More)) :- !,
  6285.         meta_predicate1(Head),
  6286.         meta_predicate(More).
  6287. meta_predicate(Head) :-
  6288.         meta_predicate1(Head).
  6289.  
  6290. meta_predicate1(Head) :-
  6291.         Head =.. [Name|Arguments],
  6292.         member(Arg, Arguments),
  6293.         module_expansion_argument(Arg), !,
  6294.         functor(Head, Name, Arity),
  6295.         module_transparent(Name/Arity).
  6296. meta_predicate1(_).             % just a mode declaration
  6297.  
  6298. module_expansion_argument(:).
  6299. module_expansion_argument(N) :- integer(N).
  6300.  
  6301. The discussion above  about the problems with the transparent  mechanism
  6302. show the two cases in which this simple transformation does not work.
  6303.  
  6304.  
  6305. CChhaapptteerr 55..  FFOORREEIIGGNN LLAANNGGUUAAGGEE IINNTTEERRFFAACCEE
  6306.  
  6307. SWI-Prolog      offers      a     powerful      interface      to      C
  6308. [Kernighan & Ritchie, 1978].     The  main  design   objectives  of  the
  6309. foreign language interface  are flexibility and performance.   A foreign
  6310. predicate  is a  C-function that  has the  same number  of arguments  as
  6311. the predicate  represented.   C-functions  are provided  to analyse  the
  6312. passed terms,  convert them to basic C-types  as well as to  instantiate
  6313. arguments using unification.   Non-deterministic foreign predicates  are
  6314. supported,  providing the  foreign  function with  a handle  to  control
  6315. backtracking.
  6316.  
  6317. C can  call Prolog  predicates, providing  both an  query interface  and
  6318. an interface  to extract  multiple solutions  from an  non-deterministic
  6319. Prolog predicate.   There is no limit  to the nesting of Prolog  calling
  6320. C, calling Prolog,  etc.  It is also  possible to write the `main'  in C
  6321. and use Prolog as an embedded logical engine.
  6322.  
  6323.  
  6324. 55..11 OOvveerrvviieeww ooff tthhee IInntteerrffaaccee
  6325.  
  6326. A special include file called SWI-Prolog.h should be  included with each
  6327. C-source file  that is  to be  loaded via the  foreign interface.    The
  6328. installation  process installs  this file  in the  directory include  in
  6329. the SWI-Prolog home directory (?- feature(home, Home).).   This C-header
  6330. file defines various data  types, macros and functions that can  be used
  6331. to communicate  with SWI-Prolog.   Functions and  macros can be  divided
  6332. into the following categories:
  6333.  
  6334.   o Analysing Prolog terms
  6335.  
  6336.   o Constructing new terms
  6337.  
  6338.   o Unifying terms
  6339.  
  6340.   o Returning control information to Prolog
  6341.  
  6342.   o Registering foreign predicates with Prolog
  6343.  
  6344.   o Calling Prolog from C
  6345.  
  6346.   o Global actions on Prolog (halt, break, abort, etc.)
  6347.  
  6348.  
  6349. 55..22 LLiinnkkiinngg FFoorreeiiggnn MMoodduulleess
  6350.  
  6351. Foreign modules  may be linked to  Prolog in three  ways.  Using  _s_t_a_t_i_c
  6352. _l_i_n_k_i_n_g,  the  extensions,  a  small  description  file  and  the  basic
  6353. SWI-Prolog object  file are linked  together to  form a new  executable.
  6354. Using _d_y_n_a_m_i_c  _l_i_n_k_i_n_g, the  extensions are linked  to a shared  library
  6355. (.so file on  most Unix systems) or  dynamic-link library (.DLL file  on
  6356. Microsoft platforms) and loaded into the the running Prolog process..
  6357.  
  6358.  
  6359. 55..22..11 WWhhaatt lliinnkkiinngg iiss pprroovviiddeedd??
  6360.  
  6361. The _s_t_a_t_i_c  _l_i_n_k_i_n_g schema can  be used on  all versions of  SWI-Prolog.
  6362. The  ffeeaattuurree//22 predicate  may be  used to  find out  what other  linking
  6363. methods are provided for this version.
  6364.  
  6365.   o _f_e_a_t_u_r_e_(_o_p_e_n___s_h_a_r_e_d___o_b_j_e_c_t_, _t_r_u_e_)
  6366.     If  this succeeds  the system  provides the  ooppeenn__sshhaarreedd__oobbjjeecctt//22and
  6367.     related predicates that  allow for handling Unix shared object files
  6368.     based  on the  Unix library functions  ddllooppeenn((2)) and  friends.   See
  6369.     section 5.4.
  6370.  
  6371.   o _f_e_a_t_u_r_e_(_d_l_l_, _t_r_u_e_)
  6372.     If  this succeeds the system provides an interface for  loading .DLL
  6373.     files by means of ooppeenn__ddllll//22 and friends.  See section 5.4.
  6374.  
  6375. If either  the feature  open_shared_object  or dll is  true, the  library
  6376. library(shlib) provides  a common  interface for  loading foreign  files
  6377. from Prolog.
  6378.  
  6379.  
  6380. 55..22..22 WWhhaatt kkiinndd ooff llooaaddiinngg sshhoouulldd II bbee uussiinngg??
  6381.  
  6382. All  described  approaches  have  their  advantages  and  disadvantages.
  6383. Static linking  is portable and allows  for debugging on all  platforms.
  6384. It is relatively  cumbersome and the libraries  you need to pass to  the
  6385. linker may vary from system to system.
  6386.  
  6387. Loading  shared objects  or DLL  files provides  sharing and  protection
  6388. and is  generally the best choice.   The  old (and also badly  portable)
  6389. ssaavvee//[[11,,22]] and ssaavvee__pprrooggrraamm//[[11,,22]] do not cooperate with  this mechanism,
  6390. but the more recent  qqssaavvee__pprrooggrraamm//[[11,,22]]can be used to  created programs
  6391. that load the appropriate library at startup.
  6392.  
  6393. Note  that  the  definition of  the  foreign  predicates  is  the  same,
  6394. regardless of the linking type used.
  6395.  
  6396.  
  6397. 55..33 DDyynnaammiicc LLiinnkkiinngg ooff sshhaarreedd lliibbrraarriieess
  6398.  
  6399. The interface  defined in this  section allows the  user to load  shared
  6400. libraries (.so files on most Unix systems).   This interface is portable
  6401. to  all machines  providing  the function  ddllooppeenn((2)) or  an  equivalent,
  6402. normally  from the  library -ldl.    These functions  provide the  basic
  6403. interface layer.   It is advised to use the predicates from  section 5.4
  6404. in your application.
  6405.  
  6406.  
  6407. ooppeenn__sshhaarreedd__oobbjjeecctt((_+_F_i_l_e_, _-_H_a_n_d_l_e))
  6408.     _F_i_l_e  is the name of a  .so file (see your C programmers  documenta-
  6409.     tion  on how to create  a .so file).   This file is attached to  the
  6410.     current  process and _H_a_n_d_l_e is unified  with a handle to the  shared
  6411.     object.    Equivalent to open_shared_object(File, [global], Handle).
  6412.     See also llooaadd__ffoorreeiiggnn__lliibbrraarryy//[[11,,22]].
  6413.  
  6414.  
  6415. ooppeenn__sshhaarreedd__oobbjjeecctt((_+_F_i_l_e_, _+_O_p_t_i_o_n_s_, _-_H_a_n_d_l_e))
  6416.     As  ooppeenn__sshhaarreedd__oobbjjeecctt//22,  but  allows  for additional  flags  to  be
  6417.     passed.   _O_p_t_i_o_n_s is a list of  atoms.  now implies the  symbols are
  6418.     resolved  immediately rather  than lazy (default).   global  implies
  6419.     symbols of the  loaded object are visible while loading other shared
  6420.     objects (by default they  are local).  Note that these flags may not
  6421.     be  supported by your operating system.  Check the  documentation of
  6422.     dlopen() or equivalent on your operating system.
  6423.  
  6424.  
  6425. cclloossee__sshhaarreedd__oobbjjeecctt((_+_H_a_n_d_l_e))
  6426.     Detach the shared object identified by _H_a_n_d_l_e.
  6427.  
  6428.  
  6429. ccaallll__sshhaarreedd__oobbjjeecctt__ffuunnccttiioonn((_+_H_a_n_d_l_e_, _+_F_u_n_c_t_i_o_n))
  6430.     Call the named function  in the loaded shared library.  The function
  6431.     is  called  without  arguments  and  the  return-value  is  ignored.
  6432.     Normally  this function installs  foreign language predicates  using
  6433.     calls to PPLL__rreeggiisstteerr__ffoorreeiiggnn(()).
  6434.  
  6435.  
  6436. 55..44 UUssiinngg tthhee lliibbrraarryy sshhlliibb ffoorr .DLL aanndd .so ffiilleess
  6437.  
  6438. This  section discusses  the  functionality  of the  (autoload)  library
  6439. shlib.pl,  providing an interface  to shared  libraries.   Currently  it
  6440. supports MS-Windows  DLL (.DLL) libraries and  Unix .so (shared  object)
  6441. files.
  6442.  
  6443.  
  6444. llooaadd__ffoorreeiiggnn__lliibbrraarryy((_+_L_i_b))
  6445.     Equivalent to load_foreign_library(Lib, install).
  6446.  
  6447.  
  6448. llooaadd__ffoorreeiiggnn__lliibbrraarryy((_+_L_i_b_, _+_E_n_t_r_y))
  6449.     Search  for the  given foreign library  and link  it to the  current
  6450.     SWI-Prolog  instance.  The library may be specified with  or without
  6451.     the  extension.     First,  aabbssoolluuttee__ffiillee__nnaammee//33is  used  to  locate
  6452.     the  file.    If  this succeeds,  the  full path  is passed  to  the
  6453.     low-level  function  to open  the library.    Otherwise,  the  plain
  6454.     library  name  is passed,  exploiting  the operating-system  defined
  6455.     search  mechanism for  the shared  library.   The  ffiillee__sseeaarrcchh__ppaatthh//22
  6456.     alias  mechanism  defines the  alias foreign,  which  refers to  the
  6457.     directories <_p_l_h_o_m_e>/lib/<_a_r_c_h>and <_p_l_h_o_m_e>/lib, in this order.
  6458.  
  6459.     If  the library  can be loaded,  the function  called _E_n_t_r_y will  be
  6460.     called  without arguments.    The return  value of  the function  is
  6461.     ignored.
  6462.  
  6463.     The  _E_n_t_r_y  function  will  normally  call  PPLL__rreeggiisstteerr__ffoorreeiiggnn(()) to
  6464.     declare functions in the library as foreign predicates.
  6465.  
  6466.  
  6467. uunnllooaadd__ffoorreeiiggnn__lliibbrraarryy((_+_L_i_b))
  6468.     If  the  foreign  library defines  the  function  uninstall(),  this
  6469.     function  will be called without  arguments and its return value  is
  6470.     ignored.    Next,  aabboolliisshh//22 is  used to  remove  all known  foreign
  6471.     predicates  defined in the library.   Finally the library itself  is
  6472.     detached from the process.
  6473.  
  6474.  
  6475. ccuurrrreenntt__ffoorreeiiggnn__lliibbrraarryy((_-_L_i_b_, _-_P_r_e_d_i_c_a_t_e_s))
  6476.     Query  the currently loaded foreign libraries and  their predicates.
  6477.     _P_r_e_d_i_c_a_t_e_s  is  a  list  with  elements  of  the  form  _M_o_d_u_l_e_:_H_e_a_d,
  6478.     indicating  the predicates installed with  PPLL__rreeggiisstteerr__ffoorreeiiggnn(())when
  6479.     the entry-point of the library was called.
  6480.  
  6481. Figure  5.1 connects  a Windows  message-box using  a foreign  function.
  6482. This example was tested using Windows NT and Microsoft Visual C++ 2.0.
  6483.  
  6484. #include <windows.h>
  6485. #include <SWI-Prolog.h>
  6486.  
  6487. static foreign_t
  6488. pl_say_hello(term_t to)
  6489. { char *a;
  6490.  
  6491.   if ( PL_get_atom_chars(to, &a) )
  6492.   { MessageBox(NULL, a, "DLL test", MB_OK|MB_TASKMODAL);
  6493.  
  6494.     PL_succeed;
  6495.   }
  6496.  
  6497.   PL_fail;
  6498. }
  6499.  
  6500. install_t
  6501. install()
  6502. { PL_register_foreign("say_hello", 1, pl_say_hello, 0);
  6503. }
  6504.  
  6505.             Figure 5.1:  MessageBox() example in Windows NT
  6506.  
  6507.  
  6508. 55..44..11 SSttaattiicc LLiinnkkiinngg
  6509.  
  6510. Below  is an  outline of  the files  structure  required for  statically
  6511. linking  SWI-Prolog with  foreign  extensions.    .../pl refers  to  the
  6512. SWI-Prolog  home  directory (see  ffeeaattuurree//22).     <_a_r_c_h> refers  to  the
  6513. architecture identifier that may be obtained using ffeeaattuurree//22.
  6514.  
  6515.       .../pl/runtime/<_a_r_c_h>/libpl.a SWI-Library
  6516.       .../pl/include/SWI-Prolog.h   Include file
  6517.       .../pl/include/SWI-Stream.h   Stream I/O include file
  6518.       .../pl/include/SWI-Exports    Export declarations (AIX only)
  6519.       .../pl/include/stub.c         Extension stub
  6520.  
  6521. The  definition   of  the  foreign  predicates   is  the  same  as   for
  6522. dynamic  linking.    Unlike  with  dynamic  linking  however,  there  is
  6523. no initialisation  function.   Instead,  the file  .../pl/include/stub.c
  6524. may  be copied  to  your project  and  modified  to define  the  foreign
  6525. extensions.   Below is  stub.c, modified to  link the lowercase  example
  6526. described later in this chapter:
  6527.  
  6528. /*  Copyright (c) 1991 Jan Wielemaker. All rights reserved.
  6529.     jan@swi.psy.uva.nl
  6530.  
  6531.     Purpose: Skeleton for extensions
  6532. */
  6533.  
  6534. #include <stdio.h>
  6535. #include <SWI-Prolog.h>
  6536.  
  6537. extern foreign_t pl_lowercase(term, term);
  6538.  
  6539. PL_extension predicates[] =
  6540. {
  6541. /*{ "name",      arity,  function,      PL_FA_<flags> },*/
  6542.  
  6543.   { "lowercase", 2       pl_lowercase,  0 },
  6544.   { NULL,        0,      NULL,          0 }     /* terminating line */
  6545. };
  6546.  
  6547. int
  6548. main(int argc, char **argv)
  6549. { PL_register_extensions(predicates);
  6550.  
  6551.   if ( !PL_initialise(argc, argv) )
  6552.     PL_halt(1);
  6553.  
  6554.   PL_install_readline();                /* delete if not required */
  6555.  
  6556.   PL_halt(PL_toplevel() ? 0 : 1);
  6557. }
  6558.  
  6559. Now, a new executable may be created by compiling  this file and linking
  6560. it to libpl.a from  the runtime directory and the libraries  required by
  6561. both the  extensions and the  SWI-Prolog kernel.   This  may be done  by
  6562. hand, or using the plld utility described in secrefplld.
  6563.  
  6564.  
  6565. 55..44..22 DDyynnaammiicc LLiinnkkiinngg bbaasseedd oonn llooaadd__ffoorreeiiggnn//[[22,,55]]
  6566.  
  6567. The  predicates  below  are considered  obsolete.     They  are  briefly
  6568. described here  for compatibility  purposes.   New code  should use  the
  6569. predicates from the library(shlib).
  6570.  
  6571.  
  6572. llooaadd__ffoorreeiiggnn((_+_F_i_l_e_, _+_E_n_t_r_y))
  6573.     Load  a  foreign file  or list  of files  specified by  _F_i_l_e.    The
  6574.     files  are searched for similar to ccoonnssuulltt//11.  Except that  the `.o'
  6575.     extension is used rather than `.pl'.
  6576.  
  6577.     _E_n_t_r_y  defines the  entry point of  the resulting  executable.   The
  6578.     entry  point  will  be  called  by Prolog  to  install  the  foreign
  6579.     predicates.
  6580.  
  6581.  
  6582. llooaadd__ffoorreeiiggnn((_+_F_i_l_e_, _+_E_n_t_r_y_, _+_O_p_t_i_o_n_s_, _+_L_i_b_r_a_r_i_e_s_, _+_S_i_z_e))
  6583.     The  first two arguments  are identical to  those of llooaadd__ffoorreeiiggnn//22.
  6584.     _O_p_t_i_o_n_s is (a list  of) additional option to be given to the loader.
  6585.     The  options are inserted  just before the files.   _L_i_b_r_a_r_i_e_s is  (a
  6586.     list  of) libraries to be passed to  the loader.  They  are inserted
  6587.     just  after the files.   If _S_i_z_e  is specified Prolog first  assumes
  6588.     that  the resulting  executable will fit  in _S_i_z_e  bytes and do  the
  6589.     loading in one pass.
  6590.  
  6591.  
  6592. ffoorreeiiggnn__ffiillee((_?_F_i_l_e))
  6593.     Is  true if  _F_i_l_e is  the absolute  path name  of a  file loaded  as
  6594.     foreign file.
  6595.  
  6596.  
  6597. 55..55 IInntteerrffaaccee DDaattaa ttyyppeess
  6598.  
  6599.  
  6600. 55..55..11 TTyyppee term_t::  aa rreeffeerreennccee ttoo aa PPrroolloogg tteerrmm
  6601.  
  6602. The principal  data-type is term_t.   Type term_t is what Quintus  calls
  6603. QP_term_ref.   This name indicates  better what the type represents:   it
  6604. is a _h_a_n_d_l_e for a  term rather than the term itself.  Terms can  only be
  6605. represented and manipulated  using this type, as  this is the only  safe
  6606. way to  ensure the  Prolog kernel is  aware of  all terms referenced  by
  6607. foreign code  and thus allows the  kernel to perform  garbage-collection
  6608. and/or stack-shifts while foreign  code is active, for example  during a
  6609. callback from C.
  6610.  
  6611. A term  reference is  a C unsigned  long, representing  the offset of  a
  6612. variable on the Prolog environment-stack.  A foreign  function is passed
  6613. term references for the predicate-arguments, one for each  argument.  If
  6614. references for intermediate  results are needed, such references  may be
  6615. created using  PPLL__nneeww__tteerrmm__rreeff(())or  PPLL__nneeww__tteerrmm__rreeffss(()).   These  references
  6616. normally live till the foreign function returns control  back to Prolog.
  6617. Their scope  can be explicitly  limited using PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(())and
  6618. PPLL__cclloossee__ffoorreeiiggnn__ffrraammee(())/PPLL__ddiissccaarrdd__ffoorreeiiggnn__ffrraammee(()).
  6619.  
  6620. A term_t  always refers to a valid Prolog term (variable,  atom, integer,
  6621. float  or compound  term).    A  term  lives either  until  backtracking
  6622. takes  us back  to a  point before  the term  was created,  the  garbage
  6623. collector  has collected  the  term or  the  term  was created  after  a
  6624. PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(())and PPLL__ddiissccaarrdd__ffoorreeiiggnn__ffrraammee(())has been called.
  6625.  
  6626. The  foreign-interface functions  can either  _r_e_a_d,  _u_n_i_f_y or  _w_r_i_t_e  to
  6627. term-references.   In the  this document we  use the following  notation
  6628. for arguments of type term_t:
  6629.  
  6630.      term_t +t  Accessed   in  read-mode.       The   `+'
  6631.                 indicates the argument is `input'.
  6632.      term_t -t  Accessed in write-mode.
  6633.      term_t ?t  Accessed in unify-mode.
  6634.  
  6635. Term references are obtained in any of the following ways.
  6636.  
  6637.   o _P_a_s_s_e_d _a_s _a_r_g_u_m_e_n_t
  6638.     The  C-functions implementing  foreign predicates  are passed  their
  6639.     arguments  as term-references.    These  references may  be read  or
  6640.     unified.  Writing to these variables causes undefined behaviour.
  6641.  
  6642.   o _C_r_e_a_t_e_d _b_y PPLL__nneeww__tteerrmm__rreeff(())
  6643.     A  term  created  by  PPLL__nneeww__tteerrmm__rreeff(())is  normally  used  to  build
  6644.     temporary  terms or be  written by one  of the interface  functions.
  6645.     For example,  PPLL__ggeett__aarrgg(())writes a reference  to the term-argument in
  6646.     its last argument.
  6647.  
  6648.   o _C_r_e_a_t_e_d _b_y PPLL__nneeww__tteerrmm__rreeffss((_i_n_t _n))
  6649.     This   function  returns  a   set  of  term   refs  with  the   same
  6650.     characteristics as PPLL__nneeww__tteerrmm__rreeff(()).  See PPLL__ooppeenn__qquueerryy(()).
  6651.  
  6652.   o _C_r_e_a_t_e_d _b_y PPLL__ccooppyy__tteerrmm__rreeff((_t_e_r_m___t _t))
  6653.     Creates a new term-reference  to the same term as the argument.  The
  6654.     term may be written to.  See figure 5.3.
  6655.  
  6656. Term-references  can safely  be  copied  to other  C-variables  of  type
  6657. term_t, but all copies will always refer to the same term.
  6658.  
  6659.  
  6660. _t_e_r_m___t PPLL__nneeww__tteerrmm__rreeff(())
  6661.     Return  a fresh reference  to a  term.   The reference is  allocated
  6662.     on  the _l_o_c_a_l  stack.   Allocating  a term-reference  may trigger  a
  6663.     stack-shift  on machines  that cannot  use sparse-memory  management
  6664.     for allocation the  Prolog stacks.  The returned reference describes
  6665.     a variable.
  6666.  
  6667.  
  6668. _t_e_r_m___t PPLL__nneeww__tteerrmm__rreeffss((_i_n_t _n))
  6669.     Return  _n  new  term  references.     The  first  term-reference  is
  6670.     returned.   The others are _t +1, _t +2, etc.   There are two reasons
  6671.     for  using this function.   PPLL__ooppeenn__qquueerryy(())expects the arguments  as
  6672.     a  set of  consecutive term references  and _v_e_r_y time-critical  code
  6673.     requiring a number of term-references can be written as:
  6674.  
  6675.     pl_mypredicate(term_t a0, term_t a1)
  6676.     { term_t t0 = PL_new_term_refs(2);
  6677.       term_t t1 = t0+1;
  6678.  
  6679.       ...
  6680.     }
  6681.  
  6682.  
  6683. _t_e_r_m___t PPLL__ccooppyy__tteerrmm__rreeff((_t_e_r_m___t _f_r_o_m))
  6684.     Create a new term  reference and make it point initially to the same
  6685.     term  as _f_r_o_m.  This function  is commonly used to copy  a predicate
  6686.     argument to a term reference that may be written.
  6687.  
  6688.  
  6689. _v_o_i_d PPLL__rreesseett__tteerrmm__rreeffss((_t_e_r_m___t _a_f_t_e_r))
  6690.     Destroy  all term  references that  have been  created after  _a_f_t_e_r,
  6691.     including  _a_f_t_e_r itself.    Any  reference to  the invalidated  term
  6692.     references after this call results in undefined behaviour.
  6693.  
  6694.     Note  that  returning  from  the  foreign  context  to  Prolog  will
  6695.     reclaim  all references  used in  the foreign  context.   This  call
  6696.     is  only necessary  if  references are  created inside  a loop  that
  6697.     never  exits  back  to  Prolog.    See  also  PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(()),
  6698.     PPLL__cclloossee__ffoorreeiiggnn__ffrraammee(())and PPLL__ddiissccaarrdd__ffoorreeiiggnn__ffrraammee(()).
  6699.  
  6700.  
  6701. 55..55..11..11 IInntteerraaccttiioonn wwiitthh tthhee ggaarrbbaaggee ccoolllleeccttoorr aanndd ssttaacckk--sshhiifftteerr
  6702.  
  6703. Prolog implements two  mechanisms for avoiding stack overflow:   garbage
  6704. collection and stack expansion.   On machines that allow for  it, Prolog
  6705. will use virtual  memory management to detect stack overflow  and expand
  6706. the  runtime stacks.    On  other machines  Prolog will  reallocate  the
  6707. stacks and update all pointers to them.  To do so,  Prolog needs to know
  6708. which data is  referenced by C-code.  As  all Prolog data known by  C is
  6709. referenced through term references (term_t), Prolog has  all information
  6710. necessary to perform  its memory management without special  precautions
  6711. from the C-programmer.
  6712.  
  6713.  
  6714. 55..55..22 OOtthheerr ffoorreeiiggnn iinntteerrffaaccee ttyyppeess
  6715.  
  6716. aattoomm__tt An atom in Prologs  internal representation.  Atoms  are pointers
  6717.     to  an  opaque structure.    They are  a  unique representation  for
  6718.     represented  text, which  implies that  atom A  represents the same
  6719.     text as atom B  if-and-only-if A and B are the same pointer.
  6720.  
  6721.     Atoms  are  the  central  representation for  textual  constants  in
  6722.     Prolog  The  transformation  of C  a  character  string to  an  atom
  6723.     implies  a hash-table lookup.  If the same atom is needed  often, it
  6724.     is  advised to  store its reference  in a  global variable to  avoid
  6725.     repeated lookup.
  6726.  
  6727. ffuunnccttoorr__tt A  functor is  the  internal  representation of  a  name/arity
  6728.     pair.   They are used to find the name and arity of  a compound term
  6729.     as  well as to construct new compound  terms.  Like atoms  they live
  6730.     for the whole Prolog session and are unique.
  6731.  
  6732. pprreeddiiccaattee__tt Handle  to a  Prolog  predicate.    Predicate  handles  live
  6733.     forever (although they can loose their definition).
  6734.  
  6735. qqiidd__tt Query         Identifier.                          Used         by
  6736.     PPLL__ooppeenn__qquueerryy(())/PPLL__nneexxtt__ssoolluuttiioonn(())/PPLL__cclloossee__qquueerryy(()) to  handle   back-
  6737.     tracking from C.
  6738.  
  6739. ffiidd__tt Frame         Identifier.                          Used         by
  6740.     PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(())/PPLL__cclloossee__ffoorreeiiggnn__ffrraammee(()).
  6741.  
  6742. mmoodduullee__tt A module is  a unique handle to a  Prolog module.  Modules  are
  6743.     used only to call predicates in a specific module.
  6744.  
  6745. ffoorreeiiggnn__tt Return type for a C-function implementing a Prolog predicate.
  6746.  
  6747. ccoonnttrrooll__tt Passed  as additional  argument to  non-deterministic  foreign
  6748.     functions.  See PL_retry*() and PL_foreign_context*().
  6749.  
  6750. iinnssttaallll__tt Type for the install() and uninstall() functions  of shared or
  6751.     dynamic link libraries.  See secrefshlib.
  6752.  
  6753.  
  6754. 55..66 TThhee FFoorreeiiggnn IInncclluuddee FFiillee
  6755.  
  6756.  
  6757. 55..66..11 AArrgguummeenntt PPaassssiinngg aanndd CCoonnttrrooll
  6758.  
  6759. If  Prolog encounters  a foreign  predicate  at run  time it  will  call
  6760. a  function  specified  in  the  predicate  definition  of  the  foreign
  6761. predicate.   The arguments 1;:::; <_a_r_i_t_y>pass the  Prolog arguments to the
  6762. goal as  Prolog terms.   Foreign  functions should  be declared of  type
  6763. foreign_t.   Deterministic foreign  functions have  two alternatives  to
  6764. return control back to Prolog:
  6765.  
  6766.  
  6767. _v_o_i_d PPLL__ssuucccceeeedd(())
  6768.     Succeed deterministically.  PL_succeed is defined as return TRUE.
  6769.  
  6770.  
  6771. _v_o_i_d PPLL__ffaaiill(())
  6772.     Fail  and  start  Prolog  backtracking.     PL_fail  is  defined  as
  6773.     return FALSE.
  6774.  
  6775.  
  6776. 55..66..11..11 NNoonn--ddeetteerrmmiinniissttiicc FFoorreeiiggnn PPrreeddiiccaatteess
  6777.  
  6778. By  default   foreign  predicates   are  deterministic.      Using   the
  6779. PL_FA_NONDETERMINISTIC   attribute  (see   PPLL__rreeggiisstteerr__ffoorreeiiggnn(()))  it   is
  6780. possible  to register  a  predicate  as a  non-deterministic  predicate.
  6781. Writing   non-deterministic   foreign  predicates   is   slightly   more
  6782. complicated  as  the  foreign function  needs  context  information  for
  6783. generating  the next  solution.   Note  that the  same foreign  function
  6784. should  be  prepared  to be  simultaneously  active  in  more  than  one
  6785. goal.     Suppose  the  natural_number_below_n/2  is  a  non-deterministic
  6786. foreign predicate, backtracking over all natural numbers  lower than the
  6787. first argument.  Now consider the following predicate:
  6788.  
  6789. quotient_below_n(Q, N) :-
  6790.         natural_number_below_n(N, N1),
  6791.         natural_number_below_n(N, N2),
  6792.         Q =:= N1 / N2, !.
  6793.  
  6794. In  this predicate  the function  natural_number_below_n/2  simultaneously
  6795. generates solutions for both its invocations.
  6796.  
  6797. Non-deterministic foreign functions  should be prepared to handle  three
  6798. different calls from Prolog:
  6799.  
  6800.   o _I_n_i_t_i_a_l _c_a_l_l _(PL_FIRST_CALL_)
  6801.     Prolog  has just created a frame  for the foreign function and  asks
  6802.     it to produce the first answer.
  6803.  
  6804.   o _R_e_d_o _c_a_l_l _(PL_REDO_)
  6805.     The previous invocation  of the foreign function associated with the
  6806.     current  goal indicated it was possible  to backtrack.  The  foreign
  6807.     function should produce the next solution.
  6808.  
  6809.   o _T_e_r_m_i_n_a_t_e _c_a_l_l _(PL_CUTTED_)
  6810.     The choice point  left by the foreign function has been destroyed by
  6811.     a  cut.  The foreign function is given the opportunity to  clean the
  6812.     environment.
  6813.  
  6814. Both  the  context  information  and  the  type  of   call  is  provided
  6815. by  an  argument  of  type  control_t  appended  to  the  argument  list
  6816. for  deterministic foreign  functions.    The macro  PPLL__ffoorreeiiggnn__ccoonnttrrooll(())
  6817. extracts  the   type  of  call   from  the  control   argument.      The
  6818. foreign  function  can  pass  a context  handle  using  the  PL_retry*()
  6819. macros  and  extract  the handle  from  the  extra  argument  using  the
  6820. PL_foreign_context*() macro.
  6821.  
  6822.  
  6823. _v_o_i_d PPLL__rreettrryy((_l_o_n_g))
  6824.     The  foreign function  succeeds while leaving  a choice  point.   On
  6825.     backtracking  over this  goal the  foreign function  will be  called
  6826.     again,  but the control argument now  indicates it is a `Redo'  call
  6827.     and the  macro PPLL__ffoorreeiiggnn__ccoonntteexxtt(())will return  the handle passed via
  6828.     PPLL__rreettrryy(()).   This handle is  a 30 bits signed  value (two bits  are
  6829.     used for status indication).
  6830.  
  6831.  
  6832. _v_o_i_d PPLL__rreettrryy__aaddddrreessss((_v_o_i_d _*))
  6833.     As  PPLL__rreettrryy(()),  but ensures an  address as  returned by malloc()  is
  6834.     correctly recovered by PPLL__ffoorreeiiggnn__ccoonntteexxtt__aaddddrreessss(()).
  6835.  
  6836.  
  6837. _i_n_t PPLL__ffoorreeiiggnn__ccoonnttrrooll((_c_o_n_t_r_o_l___t))
  6838.     Extracts  the type of  call from the control  argument.  The  return
  6839.     values  are  described above.    Note that  the  function should  be
  6840.     prepared  to handle the PL_CUTTED case and should be aware  that the
  6841.     other arguments are not valid in this case.
  6842.  
  6843.  
  6844. _l_o_n_g PPLL__ffoorreeiiggnn__ccoonntteexxtt((_c_o_n_t_r_o_l___t))
  6845.     Extracts  the context from the context  argument.  In the call  type
  6846.     is  PL_FIRST_CALL the context value is 0L. Otherwise it  is the value
  6847.     returned  by the last PPLL__rreettrryy(()) associated with this goal (both  if
  6848.     the call type is PL_REDO as PL_CUTTED).
  6849.  
  6850.  
  6851. _v_o_i_d _* PPLL__ffoorreeiiggnn__ccoonntteexxtt__aaddddrreessss((_c_o_n_t_r_o_l___t))
  6852.     Extracts an address as passed in by PPLL__rreettrryy__aaddddrreessss(()).
  6853.  
  6854. Note:  If a  non-deterministic foreign function returns using PL_succeed
  6855. or  PL_fail,   Prolog  assumes  the  foreign  function  has  cleaned  its
  6856. environment.  NNoo call with control argument PL_CUTTED will follow.
  6857.  
  6858. The code of figure 5.2 shows a skeleton for  a non-deterministic foreign
  6859. predicate definition.
  6860.  
  6861. typedef struct                  /* define a context structure */
  6862. { ...
  6863. } context;
  6864.  
  6865. foreign_t
  6866. my_function(term_t a0, term_t a1, foreign_t handle)
  6867. { struct context * ctxt;
  6868.  
  6869.   switch( PL_foreign_control(handle) )
  6870.   { case PL_FIRST_CALL:
  6871.         ctxt = malloc(sizeof(struct context));
  6872.         ...
  6873.         PL_retry_address(ctxt);
  6874.     case PL_REDO:
  6875.  
  6876.         ctxt = PL_foreign_context_address(handle);
  6877.         ...
  6878.         PL_retry_address(ctxt);
  6879.     case PL_CUTTED:
  6880.         free(ctxt);
  6881.         PL_succeed;
  6882.   }
  6883. }
  6884.  
  6885.      Figure 5.2:  Skeleton for non-deterministic foreign functions
  6886.  
  6887.  
  6888. 55..66..22 AAttoommss aanndd ffuunnccttoorrss
  6889.  
  6890. The  following  functions  provide for  communication  using  atoms  and
  6891. functors.
  6892.  
  6893.  
  6894. _a_t_o_m___t PPLL__nneeww__aattoomm((_c_o_n_s_t _c_h_a_r _*))
  6895.     Return an atom handle  for the given C-string.  This function always
  6896.     succeeds.  The returned handle is valid for the entire session.
  6897.  
  6898.  
  6899. _c_o_n_s_t _c_h_a_r _* PPLL__aattoomm__cchhaarrss((_a_t_o_m___t _a_t_o_m))
  6900.     Return  a C-string for the text represented by the given atom.   The
  6901.     returned  text will not be changed by Prolog.  It is  not allowed to
  6902.     modify  the contents, not even `temporary' as the string  may reside
  6903.     in read-only memory.
  6904.  
  6905.  
  6906. _f_u_n_c_t_o_r___t PPLL__nneeww__ffuunnccttoorr((_a_t_o_m___t _n_a_m_e_, _i_n_t _a_r_i_t_y))
  6907.     Returns  a _f_u_n_c_t_o_r  _i_d_e_n_t_i_f_i_e_r, a  handle for  the name/arity  pair.
  6908.     The returned handle is valid for the entire Prolog session.
  6909.  
  6910.  
  6911. _a_t_o_m___t PPLL__ffuunnccttoorr__nnaammee((_f_u_n_c_t_o_r___t _f))
  6912.     Return an atom representing the name of the given functor.
  6913.  
  6914.  
  6915. _i_n_t PPLL__ffuunnccttoorr__aarriittyy((_f_u_n_c_t_o_r___t _f))
  6916.     Return the arity of the given functor.
  6917.  
  6918.  
  6919. 55..66..33 AAnnaallyyssiinngg TTeerrmmss vviiaa tthhee FFoorreeiiggnn IInntteerrffaaccee
  6920.  
  6921. Each argument  of a foreign function  (except for the control  argument)
  6922. is of type term_t, an opaque  handle to a Prolog term.  Three  groups of
  6923. functions are  available for  the analysis  of terms.    The first  just
  6924. validates the type,  like the Prolog  predicates vvaarr//11, aattoomm//11, etc  and
  6925. are  called PL_is_*().    The second  group  attempts to  translate  the
  6926. argument into a C primitive type.   These predicates take a term_t and a
  6927. pointer to the appropriate C-type and return TRUE or  FALSE depending on
  6928. successful or unsuccessful translation.   If the translation fails,  the
  6929. pointed-to data is never modified.
  6930.  
  6931.  
  6932. 55..66..33..11 TTeessttiinngg tthhee ttyyppee ooff aa tteerrmm
  6933.  
  6934.  
  6935. _i_n_t PPLL__tteerrmm__ttyyppee((_t_e_r_m___t))
  6936.     Obtain  the type  of a  term,  which should  be a  term returned  by
  6937.     one  of the  other interface  predicates or passed  as an  argument.
  6938.     The  function  returns the  type  of  the Prolog  term.    The  type
  6939.     identifiers  are listed below.   Note that the extraction  functions
  6940.     PL_ge_t*() also validate  the type and  thus the two sections  below
  6941.     are equivalent.
  6942.  
  6943.             if ( PL_is_atom(t) )
  6944.             { char *s;
  6945.  
  6946.               PL_get_atom_chars(t, &s);
  6947.               ...;
  6948.             }
  6949.  
  6950.     or
  6951.  
  6952.             char *s;
  6953.             if ( PL_get_atom_chars(t, &s) )
  6954.             { ...;
  6955.             }
  6956.  
  6957.     ___________________________________________________________________
  6958.     | PL_VARIABLE            |An unbound variable.   The value  of term|
  6959.     |                        |as such  is a  unique identifier  for the|
  6960.  
  6961.     |                        |variable.                                |
  6962.     | PL_ATOM                |A Prolog atom.                           |
  6963.     | PL_STRING              |A Prolog string.                         |
  6964.     | PL_INTEGER             |A Prolog integer.                        |
  6965.     | PL_FLOAT               |A Prolog floating point number.          |
  6966.     | PL_TERM                |A compound term.   Note that a  list is a|
  6967.     |________________________|compound_term_.//22._______________________|
  6968.  
  6969. The functions PL_is_<_t_y_p_e> are an alternative to PPLL__tteerrmm__ttyyppee(()).  The test
  6970. PPLL__iiss__vvaarriiaabbllee((_t_e_r_m))is  equivalent  to PPLL__tteerrmm__ttyyppee((_t_e_r_m))== PL_VARIABLE,
  6971. but  the first  is considerably  faster.   On  the other  hand, using  a
  6972. switch over  PPLL__tteerrmm__ttyyppee(())is  faster and  more readable  then using  an
  6973. if-then-else using  the functions  below.   All  these functions  return
  6974. either TRUE or FALSE.
  6975.  
  6976.  
  6977. _i_n_t PPLL__iiss__vvaarriiaabbllee((_t_e_r_m___t))
  6978.     Returns non-zero if _t_e_r_m is a variable.
  6979.  
  6980.  
  6981. _i_n_t PPLL__iiss__aattoomm((_t_e_r_m___t))
  6982.     Returns non-zero if _t_e_r_m is an atom.
  6983.  
  6984.  
  6985. _i_n_t PPLL__iiss__ssttrriinngg((_t_e_r_m___t))
  6986.     Returns non-zero if _t_e_r_m is a string.
  6987.  
  6988.  
  6989. _i_n_t PPLL__iiss__iinntteeggeerr((_t_e_r_m___t))
  6990.     Returns non-zero if _t_e_r_m is an integer.
  6991.  
  6992.  
  6993. _i_n_t PPLL__iiss__ffllooaatt((_t_e_r_m___t))
  6994.     Returns non-zero if _t_e_r_m is a float.
  6995.  
  6996.  
  6997. _i_n_t PPLL__iiss__ccoommppoouunndd((_t_e_r_m___t))
  6998.     Returns non-zero if _t_e_r_m is a compound term.
  6999.  
  7000.  
  7001. _i_n_t PPLL__iiss__ffuunnccttoorr((_t_e_r_m___t_, _f_u_n_c_t_o_r___t))
  7002.     Returns  non-zero if _t_e_r_m  is compound and  its functor is  _f_u_n_c_t_o_r.
  7003.     This test  is equivalent to PPLL__ggeett__ffuunnccttoorr(()),  followed by testing the
  7004.     functor, but easier to write and faster.
  7005.  
  7006.  
  7007. _i_n_t PPLL__iiss__lliisstt((_t_e_r_m___t))
  7008.     Returns non-zero if _t_e_r_m  is a compound term with functor ./2 or the
  7009.     atom [].
  7010.  
  7011.  
  7012. _i_n_t PPLL__iiss__aattoommiicc((_t_e_r_m___t))
  7013.     Returns non-zero if _t_e_r_m is atomic (not variable or compound).
  7014.  
  7015.  
  7016. _i_n_t PPLL__iiss__nnuummbbeerr((_t_e_r_m___t))
  7017.     Returns non-zero if _t_e_r_m is an integer or float.
  7018.  
  7019.  
  7020. 55..66..33..22 RReeaaddiinngg ddaattaa ffrroomm aa tteerrmm
  7021.  
  7022. The functions PL_get_*() read information from  a Prolog term.  Most  of
  7023. them take two arguments.  The first is the input  term and the second is
  7024. a pointer to the output value or a term-reference.
  7025.  
  7026.  
  7027. _i_n_t PPLL__ggeett__aattoomm((_t_e_r_m___t _+_t_, _a_t_o_m___t _*_a))
  7028.     If _t is an atom,  store the unique atom identifier over _a.  See also
  7029.     PPLL__aattoomm__cchhaarrss(())and PPLL__nneeww__aattoomm(()).  If there is no need  to access the
  7030.     data  (characters) of  an atom,  it is advised  to manipulate  atoms
  7031.     using their handle.
  7032.  
  7033.  
  7034. _i_n_t PPLL__ggeett__aattoomm__cchhaarrss((_t_e_r_m___t _+_t_, _c_h_a_r _*_*_s))
  7035.     If  _t is an atom, store a  pointer to a 0-terminated C-string  in _s.
  7036.     It is explicitly  nnoott allowed to modify the contents of this string.
  7037.     Some  built-in  atoms may  have the  string  allocated in  read-only
  7038.     memory, so `temporary manipulation' can cause an error.
  7039.  
  7040.  
  7041. _i_n_t PPLL__ggeett__ssttrriinngg((_t_e_r_m___t _+_t_, _c_h_a_r _*_*_s_, _i_n_t _*_l_e_n))
  7042.     If  _t  is  a  string  object,  store a  pointer  to  a  0-terminated
  7043.     C-string  in _s  and the  length of  the string in  _l_e_n.   Note  that
  7044.     this pointer  is invalidated by backtracking, garbage-collection and
  7045.     stack-shifts,  so generally the only save operations are to  pass it
  7046.     immediately to a C-function that doesn't involve Prolog.
  7047.  
  7048.  
  7049. _i_n_t PPLL__ggeett__cchhaarrss((_t_e_r_m___t _+_t_, _c_h_a_r _*_*_s_, _u_n_s_i_g_n_e_d _f_l_a_g_s))
  7050.     Convert  the argument term _t to  a 0-terminated C-string.  _f_l_a_g_s  is
  7051.     a  bitwise disjunction  from two  groups of  constants.   The  first
  7052.     specifies  which term-types should converted and the second  how the
  7053.     argument  is stored.  Below  is a specification of these  constants.
  7054.     BUF_RING implies, if  the data is not static (as from an  atom), the
  7055.     data  is copied to the next buffer from a ring of four  (4) buffers.
  7056.     This is a  convenient way of converting multiple arguments passed to
  7057.     a  foreign predicate to C-strings.   If BUF_MALLOC is used,  the data
  7058.     must be freed using free() when not needed any longer.
  7059.     ___________________________________________________________________
  7060.     | CVT_ATOM               |Convert if term is an atom               |
  7061.     | CVT_STRING             |Convert if term is a string              |
  7062.     | CVT_LIST               |Convert  if term  is a  list of  integers|
  7063.     |                        |between 1 and 255                        |
  7064.  
  7065.     | CVT_INTEGER            |Convert if term is an integer (using %d) |
  7066.     | CVT_FLOAT              |Convert if term is a float (using %f)    |
  7067.     | CVT_NUMBER             |Convert if term is a integer or float    |
  7068.     | CVT_ATOMIC             |Convert if term is atomic                |
  7069.     | CVT_VARIABLE           |Convert variable to print-name           |
  7070.     | CVT_ALL                |Convert  if term  is  any  of the  above,|
  7071.     |________________________|except_for_variables_____________________|
  7072.     | BUF_DISCARDABLE        |Data must copied immediately             |
  7073.  
  7074.     | BUF_RING               |Data is stored in a ring of buffers      |
  7075.     | BUF_MALLOC             |Data is  copied to a new  buffer returned||
  7076.     |________________________________________________|by__mmaalllloocc((3))__________________________________________________________||
  7077.  
  7078.  
  7079. _i_n_t PPLL__ggeett__lliisstt__cchhaarrss((_+_t_e_r_m___t _l_, _c_h_a_r _*_*_s_, _u_n_s_i_g_n_e_d _f_l_a_g_s))
  7080.     Same  as PPLL__ggeett__cchhaarrss((_l_, _s_, _C_V_T___L_I_S_T___f_l_a_g_s)),  provided _f_l_a_g_s  contains
  7081.     no of the CVT_* flags.
  7082.  
  7083.  
  7084. _i_n_t PPLL__ggeett__iinntteeggeerr((_+_t_e_r_m___t _t_, _i_n_t _*_i))
  7085.     If  _t is  a Prolog  integer, assign  its value over  _i.   On  32-bit
  7086.     machines,  this is  the same as  PPLL__ggeett__lloonngg(()),  but avoids a  warning
  7087.     from the compiler.  See also PPLL__ggeett__lloonngg(()).
  7088.  
  7089.  
  7090. _i_n_t PPLL__ggeett__lloonngg((_t_e_r_m___t _+_t_, _l_o_n_g _*_i))
  7091.     If  _t is  a Prolog  integer, assign  its value over  _i.   Note  that
  7092.     Prolog integers have limited  value-range.  If _t is a floating point
  7093.     number that can  be represented as a long, this function succeeds as
  7094.     well.
  7095.  
  7096.  
  7097. _i_n_t PPLL__ggeett__ppooiinntteerr((_t_e_r_m___t _+_t_, _v_o_i_d _*_*_p_t_r))
  7098.     In   the  current  system,   pointers  are  represented  by   Prolog
  7099.     integers,   but  need  some  manipulation  to  make  sure   they  do
  7100.     not  get  truncated   due  to  the  limited  Prolog  integer  range.
  7101.     PPLL__ppuutt__ppooiinntteerr(())/PPLL__ggeett__ppooiinntteerr(())guarantees pointers in the  range of
  7102.     malloc() are handled without truncating.
  7103.  
  7104.  
  7105. _i_n_t PPLL__ggeett__ffllooaatt((_t_e_r_m___t _+_t_, _d_o_u_b_l_e _*_f))
  7106.     If _t is a float or integer, its value is assigned over _f.
  7107.  
  7108.  
  7109. _i_n_t PPLL__ggeett__ffuunnccttoorr((_t_e_r_m___t _+_t_, _f_u_n_c_t_o_r___t _*_f))
  7110.     If  _t  is  compound  or  an  atom,   the  Prolog  representation  of
  7111.     the   name-arity  pair  will  be  assigned   over  _f.     See   also
  7112.     PPLL__ggeett__nnaammee__aarriittyy(())and PPLL__iiss__ffuunnccttoorr(()).
  7113.  
  7114.  
  7115. _i_n_t PPLL__ggeett__nnaammee__aarriittyy((_t_e_r_m___t _+_t_, _a_t_o_m___t _*_n_a_m_e_, _i_n_t _*_a_r_i_t_y))
  7116.     If  _t is  compound or  an atom,  the functor-name  will be  assigned
  7117.     over  _n_a_m_e and the arity over  _a_r_i_t_y.  See  also PPLL__ggeett__ffuunnccttoorr(())and
  7118.     PPLL__iiss__ffuunnccttoorr(()).
  7119.  
  7120.  
  7121. _i_n_t PPLL__ggeett__mmoodduullee((_t_e_r_m___t _+_t_, _m_o_d_u_l_e___t _*_m_o_d_u_l_e))
  7122.     If _t is an  atom, the system will lookup or create the corresponding
  7123.     module and assign an opaque pointer to it over _m_o_d_u_l_e,.
  7124.  
  7125.  
  7126. _i_n_t PPLL__ggeett__aarrgg((_i_n_t _i_n_d_e_x_, _t_e_r_m___t _+_t_, _t_e_r_m___t _-_a))
  7127.     If  _t is  compound and  index is  between 1  and arity  (including),
  7128.     assign _a with a term-reference to the argument.
  7129.  
  7130.  
  7131. 55..66..33..33 RReeaaddiinngg aa lliisstt
  7132.  
  7133. The functions from this section are intended to read  a Prolog list from
  7134. C. Suppose we expect a list of atoms, the following  code will print the
  7135. atoms, each on a line:
  7136.  
  7137. foreign_t
  7138. pl_write_atoms(term_t l)
  7139. { term_t head = PL_new_term_ref();      /* variable for the elements */
  7140.   term_t list = PL_copy_term_ref();     /* copy as we need to write */
  7141.  
  7142.   while( PL_get_list(list, head, list) )
  7143.   { char *s;
  7144.  
  7145.     if ( PL_get_atom_chars(head, &s) )
  7146.       Sprintf("%s\n", s);
  7147.     else
  7148.       PL_fail;
  7149.   }
  7150.  
  7151.   return PL_get_nil(list);              /* test end for [] */
  7152. }
  7153.  
  7154.  
  7155. _i_n_t PPLL__ggeett__lliisstt((_t_e_r_m___t _+_l_, _t_e_r_m___t _-_h_, _t_e_r_m___t _-_t))
  7156.     If  _l is a list and not [] assign a term-reference to the  head to _h
  7157.     and to the tail to _t.
  7158.  
  7159.  
  7160. _i_n_t PPLL__ggeett__hheeaadd((_t_e_r_m___t _+_l_, _t_e_r_m___t _-_h))
  7161.     If _l is a list and not [] assign a term-reference to the head to _h.
  7162.  
  7163.  
  7164. _i_n_t PPLL__ggeett__ttaaiill((_t_e_r_m___t _+_l_, _t_e_r_m___t _-_t))
  7165.     If _l is a list and not [] assign a term-reference to the tail to _t.
  7166.  
  7167.  
  7168. _i_n_t PPLL__ggeett__nniill((_t_e_r_m___t _+_l))
  7169.     Succeeds if  represents the atom [].
  7170.  
  7171.  
  7172. 55..66..33..44 AAnn eexxaammppllee::  ddeeffiinniinngg ddiissppllaayy//11 iinn CC
  7173.  
  7174. Figure 5.3 shows  a definition of ddiissppllaayy//11 to illustrate  the described
  7175. functions.
  7176. foreign_t
  7177. pl_display(term_t t)
  7178. { functor_t functor;
  7179.  
  7180.   int arity, len, n;
  7181.   char *s;
  7182.  
  7183.   switch( PL_term_type(t) )
  7184.   { case PL_VARIABLE:
  7185.     case PL_ATOM:
  7186.     case PL_INTEGER:
  7187.     case PL_FLOAT:
  7188.  
  7189.       PL_get_chars(t, &s, CVT_ALL);
  7190.       Sprintf("%s", s);
  7191.       break;
  7192.     case PL_STRING:
  7193.       PL_get_string_chars(t, &s, &len);
  7194.       Sprintf("\"%s\"", s);
  7195.       break;
  7196.  
  7197.     case PL_TERM:
  7198.     { term_t a = PL_new_term_ref();
  7199.  
  7200.       PL_get_name_arity(t, &name, &arity);
  7201.       Sprintf("%s(", PL_atom_chars(name));
  7202.       for(n=1; n<=arity; n++)
  7203.       { PL_get_arg(n, t, a);
  7204.         if ( n > 1 )
  7205.  
  7206.           Sprintf(", ");
  7207.         pl_display(a);
  7208.       }
  7209.       Sprintf(")");
  7210.       break;
  7211.     default:
  7212.       PL_fail;                          /* should not happen */
  7213.  
  7214.   }
  7215.  
  7216.   PL_succeed;
  7217. }
  7218.  
  7219.              Figure 5.3:  A Foreign definition of ddiissppllaayy//11
  7220.  
  7221.  
  7222. 55..66..44 CCoonnssttrruuccttiinngg TTeerrmmss
  7223.  
  7224. Terms  can  be  constructed  using  functions from  the  PL_put_*()  and
  7225. PL_cons_*()  families.    This  approach  builds the  term  `inside-out',
  7226. starting  at  the  leaves  and  subsequently  creating  compound  terms.
  7227. Alternatively,   terms  may  be  created   `top-down',  first   creating
  7228. a  compound  holding  only  variables  and   subsequently  unifying  the
  7229. arguments.   This section  discusses functions  for the first  approach.
  7230. This approach is generally used for creating arguments for PPLL__ccaallll(()) and
  7231. PL_open_query.
  7232.  
  7233.  
  7234. _v_o_i_d PPLL__ppuutt__vvaarriiaabbllee((_t_e_r_m___t _-_t))
  7235.     Put  a fresh variable in  the term.   The new variable lives on  the
  7236.     global  stack.   Note that the initial  variable lives on the  local
  7237.     stack  and is  lost after  a write to  the term-references.    After
  7238.     using this function, the variable will continue to live.
  7239.  
  7240.  
  7241. _v_o_i_d PPLL__ppuutt__aattoomm((_t_e_r_m___t _-_t_, _a_t_o_m___t _a))
  7242.     Put  an  atom  in the  term  reference from  a  handle.    See  also
  7243.     PPLL__nneeww__aattoomm(())and PPLL__aattoomm__cchhaarrss(()).
  7244.  
  7245.  
  7246. _v_o_i_d PPLL__ppuutt__aattoomm__cchhaarrss((_t_e_r_m___t _-_t_, _c_o_n_s_t _c_h_a_r _*_c_h_a_r_s))
  7247.     Put an atom  in the term-reference constructed from the 0-terminated
  7248.     string.  The  string itself will never be references by Prolog after
  7249.     this function.
  7250.  
  7251.  
  7252. _v_o_i_d PPLL__ppuutt__ssttrriinngg__cchhaarrss((_t_e_r_m___t _-_t_, _c_o_n_s_t _c_h_a_r _*_c_h_a_r_s))
  7253.     Put a string in the term-reference.  The data will be copied.
  7254.  
  7255.  
  7256. _v_o_i_d PPLL__ppuutt__lliisstt__cchhaarrss((_t_e_r_m___t _-_t_, _c_o_n_s_t _c_h_a_r _*_c_h_a_r_s))
  7257.     Put a list of ASCII values in the term-reference.
  7258.  
  7259.  
  7260. _v_o_i_d PPLL__ppuutt__iinntteeggeerr((_t_e_r_m___t _-_t_, _l_o_n_g _i))
  7261.     Put a Prolog integer in the term reference.
  7262.  
  7263.  
  7264. _v_o_i_d PPLL__ppuutt__ppooiinntteerr((_t_e_r_m___t _-_t_, _v_o_i_d _*_p_t_r))
  7265.     Put a Prolog integer  in the term-reference.  Provided ptr is in the
  7266.     `malloc()-area', PPLL__ggeett__ppooiinntteerr(())will get the pointer back.
  7267.  
  7268.  
  7269. _v_o_i_d PPLL__ppuutt__ffllooaatt((_t_e_r_m___t _-_t_, _d_o_u_b_l_e _f))
  7270.     Put a floating-point value in the term-reference.
  7271.  
  7272.  
  7273. _v_o_i_d PPLL__ppuutt__ffuunnccttoorr((_t_e_r_m___t _-_t_, _f_u_n_c_t_o_r___t _f_u_n_c_t_o_r))
  7274.     Create  a new compound  term from _f_u_n_c_t_o_r and  bind _t to this  term.
  7275.     All arguments of the  term will be variables.  To create a term with
  7276.     instantiated  arguments, either instantiate the arguments  using the
  7277.     PL_unify_*() functions or use PPLL__ccoonnss__ffuunnccttoorr(()).
  7278.  
  7279.  
  7280. _v_o_i_d PPLL__ppuutt__lliisstt((_t_e_r_m___t _-_l))
  7281.     Same as PPLL__ppuutt__ffuunnccttoorr((_l_, _P_L___n_e_w___f_u_n_c_t_o_r_(_P_L___n_e_w___a_t_o_m_(_"_._")), 2)).
  7282.  
  7283.  
  7284. _v_o_i_d PPLL__ppuutt__nniill((_t_e_r_m___t _-_l))
  7285.     Same as PPLL__ppuutt__aattoomm__cchhaarrss((_"_[_]_")).
  7286.  
  7287.  
  7288. _v_o_i_d PPLL__ppuutt__tteerrmm((_t_e_r_m___t _-_t_1_, _t_e_r_m___t _+_t_2))
  7289.     Make _t_1 point to the same term as _t_2.
  7290.  
  7291.  
  7292. _v_o_i_d PPLL__ccoonnss__ffuunnccttoorr((_t_e_r_m___t _-_h_, _f_u_n_c_t_o_r___t _f_, _._._.))
  7293.     Create  a term,  whose arguments are  filled from variable  argument
  7294.     list  holding the same number of term_t objects as the arity  of the
  7295.     functor.  To create the term animal(gnu, 50), use:
  7296.  
  7297.             term_t a1 = PL_new_term_ref();
  7298.             term_t a2 = PL_new_term_ref();
  7299.             term_t t;
  7300.  
  7301.             PL_put_atom_chars(a1, "gnu");
  7302.             PL_put_integer(a2, 50);
  7303.             PL_cons_functor(t, PL_new_functor(PL_new_atom("animal"), 2),
  7304.                             a1, a2);
  7305.  
  7306.     After  this sequence, the term-references _a_1 and _a_2 may be  used for
  7307.     other purposes.
  7308.  
  7309.  
  7310. _v_o_i_d PPLL__ccoonnss__lliisstt((_t_e_r_m___t _-_l_, _t_e_r_m___t _+_h_, _t_e_r_m___t _+_t))
  7311.     Create  a list (cons-) cell in _l from  the head and tail.   The code
  7312.     below  creates a list of  atoms from a char **.   The list is  built
  7313.     tail-to-head.   The  PL_unify_*()  functions can be  used to build  a
  7314.     list head-to-tail.
  7315.  
  7316.     void
  7317.     put_list(term_t l, int n, char **words)
  7318.     { term_t a = PL_new_term_ref();
  7319.  
  7320.       PL_put_nil(l);
  7321.       while( --n >= 0 )
  7322.       { PL_put_atom_chars(a, words[n]);
  7323.         PL_put_list(l, a, l);
  7324.       }
  7325.     }
  7326.  
  7327.  
  7328. 55..66..55 UUnniiffyyiinngg ddaattaa
  7329.  
  7330. The  functions  of  this  sections  _u_n_i_f_y  terms  with  other  terms  or
  7331. translated C-data structures.   Except for  PPLL__uunniiffyy(()), the functions  of
  7332. this section  are specific  to SWI-Prolog.   They  have been  introduced
  7333. to make translation  of old code easier,  but also because they  provide
  7334. for a faster mechanism  for returning data to Prolog that  requires less
  7335. term-references.  Consider the case where we want  a foreign function to
  7336. return the  host name of the  machine Prolog is running  on.  Using  the
  7337. PL_get_*() and PL_put_*() functions, the code becomes:
  7338.  
  7339. foreign_t
  7340. pl_hostname(term_t name)
  7341. { char buf[100];
  7342.  
  7343.   if ( gethostname(buf, sizeof(buf)) )
  7344.   { term_t tmp = PL_new_term_ref();
  7345.  
  7346.     PL_put_atom_chars(tmp, buf);
  7347.     return PL_unify(name, buf);
  7348.   }
  7349.  
  7350.   PL_fail;
  7351. }
  7352.  
  7353. Using PPLL__uunniiffyy__aattoomm__cchhaarrss(()), this becomes:
  7354.  
  7355. foreign_t
  7356. pl_hostname(term_t name)
  7357. { char buf[100];
  7358.  
  7359.   if ( gethostname(buf, sizeof(buf)) )
  7360.     return PL_unify_atom_chars(name, buf);
  7361.  
  7362.   PL_fail;
  7363. }
  7364.  
  7365.  
  7366. _i_n_t PPLL__uunniiffyy((_t_e_r_m___t _?_t_1_, _t_e_r_m___t _?_t_2))
  7367.     Unify two Prolog terms and return non-zero on success.
  7368.  
  7369.  
  7370. _i_n_t PPLL__uunniiffyy__aattoomm((_t_e_r_m___t _?_t_, _a_t_o_m___t _a))
  7371.     Unify _t with the atom _a and return non-zero on success.
  7372.  
  7373.  
  7374. _i_n_t PPLL__uunniiffyy__aattoomm__cchhaarrss((_t_e_r_m___t _?_t_, _c_o_n_s_t _c_h_a_r _*_c_h_a_r_s))
  7375.     Unify  _t with  an atom  created from  _c_h_a_r_s and  return non-zero  on
  7376.     success.
  7377.  
  7378.  
  7379. _i_n_t PPLL__uunniiffyy__lliisstt__cchhaarrss((_t_e_r_m___t _?_t_, _c_o_n_s_t _c_h_a_r _*_c_h_a_r_s))
  7380.     Unify _t with a list of ASCII characters constructed from _c_h_a_r_s.
  7381.  
  7382.  
  7383. _i_n_t PPLL__uunniiffyy__ssttrriinngg__cchhaarrss((_t_e_r_m___t _?_t_, _c_o_n_s_t _c_h_a_r _*_c_h_a_r_s))
  7384.     Unify _t with a Prolog string object created from _c_h_a_r_s.
  7385.  
  7386.  
  7387. _i_n_t PPLL__uunniiffyy__iinntteeggeerr((_t_e_r_m___t _?_t_, _l_o_n_g _n))
  7388.     Unify _t with a Prolog integer from _n.
  7389.  
  7390.  
  7391. _i_n_t PPLL__uunniiffyy__ffllooaatt((_t_e_r_m___t _?_t_, _d_o_u_b_l_e _f))
  7392.     Unify _t with a Prolog float from _f.
  7393.  
  7394.  
  7395. _i_n_t PPLL__uunniiffyy__ppooiinntteerr((_t_e_r_m___t _?_t_, _v_o_i_d _*_p_t_r))
  7396.     Unify  _t with a  Prolog integer  describing the pointer.   See  also
  7397.     PPLL__ppuutt__ppooiinntteerr(())and PPLL__ggeett__ppooiinntteerr(()).
  7398.  
  7399.  
  7400. _i_n_t PPLL__uunniiffyy__ffuunnccttoorr((_t_e_r_m___t _?_t_, _f_u_n_c_t_o_r___t _f))
  7401.     If  _t is a compound term with  the given functor, just succeed.   If
  7402.     it  is unbound,  create a term  and bind the  variable, else  fails.
  7403.     Not  that this function  does not create a  term if the argument  is
  7404.     already instantiated.
  7405.  
  7406.  
  7407. _i_n_t PPLL__uunniiffyy__lliisstt((_t_e_r_m___t _?_l_, _t_e_r_m___t _-_h_, _t_e_r_m___t _-_t))
  7408.     Unify  _l with a list-cell (./2).   If successful, write  a reference
  7409.     to  the head of  the list to _h  and a reference  to the tail of  the
  7410.     list  in _t.   This  reference may  be used for  subsequent calls  to
  7411.     this  function.   Suppose we  want to  return a list  of atoms  from
  7412.     a  char **.   We could  use the  example described  by PPLL__ppuutt__lliisstt(()),
  7413.     followed by  a call to PPLL__uunniiffyy(()), or we can use the code  below.  If
  7414.     the  predicate argument is unbound,  the difference is minimal  (the
  7415.     code  based on PPLL__ppuutt__lliisstt(())is  probably slightly faster).   If  the
  7416.     argument  is bound, the code below may fail before reaching  the end
  7417.     of  the word-list, but even  if the unification succeeds, this  code
  7418.     avoids a duplicate (garbage) list and a deep unification.
  7419.  
  7420.     foreign_t
  7421.     pl_get_environ(term_t env)
  7422.     { term_t l = PL_copy_term_ref(env);
  7423.       term_t a = PL_new_term_ref();
  7424.       extern char **environ;
  7425.  
  7426.       while(*environ)
  7427.       { if ( !PL_unify_list(l, a, l) ||
  7428.              !PL_unify_atom_chars(a, *environ) )
  7429.           PL_fail;
  7430.       }
  7431.  
  7432.       return PL_unify_nil(l);
  7433.     }
  7434.  
  7435.  
  7436. _i_n_t PPLL__uunniiffyy__nniill((_t_e_r_m___t _?_l))
  7437.     Unify _l with the atom [].
  7438.  
  7439.  
  7440. _i_n_t PPLL__uunniiffyy__aarrgg((_i_n_t _i_n_d_e_x_, _t_e_r_m___t _?_t_, _t_e_r_m___t _?_a))
  7441.     Unifies the _i_n_d_e_x_-_t_h argument (1-based) of _t with _a.
  7442.  
  7443.  
  7444. _i_n_t PPLL__uunniiffyy__tteerrmm((_t_e_r_m___t _?_t_, _._._.))
  7445.     Unify  _t with a (normally) compound  term.  The remaining  arguments
  7446.     is  a  sequence  of a  type  identifier,  followed by  the  required
  7447.     arguments.      This  predicate  is  an  extension  to  the  Quintus
  7448.     and  SICStus foreign  interface  from which  the SWI-Prolog  foreign
  7449.     interface  has been  derived, but has  proved to  be a powerful  and
  7450.     comfortable  way to create compound terms from C. Due to  the vararg
  7451.     packing/unpacking and  the required type-switching this interface is
  7452.     slightly  slower than using the primitives.   Please note that  some
  7453.     bad  C-compilers have fairly low  limits on the number of  arguments
  7454.     that may be passed to a function.
  7455.  
  7456.     The type identifiers are:
  7457.  
  7458.     PL_VARIABLE nnoonnee
  7459.          No op.  Used in arguments of PL_FUNCTOR.
  7460.  
  7461.     PL_ATOM aattoomm__tt
  7462.          Unify the argument with an atom, as in PPLL__uunniiffyy__aattoomm(()).
  7463.  
  7464.     PL_INTEGER lloonngg
  7465.          Unify the argument with an integer, as in PPLL__uunniiffyy__iinntteeggeerr(()).
  7466.  
  7467.     PL_FLOAT ddoouubbllee
  7468.          Unify  the  argument  with  a  float,  as  in  PPLL__uunniiffyy__ffllooaatt(()).
  7469.          Note  that,  as the  argument  is  passed using  the  C  vararg
  7470.          conventions, a float must be casted to a double explicitly.
  7471.  
  7472.     PL_STRING ccoonnsstt cchhaarr **
  7473.          Unify   the   argument   with   a   string   object,    as   in
  7474.          PPLL__uunniiffyy__ssttrriinngg__cchhaarrss(()).
  7475.  
  7476.     PL_TERM tteerrmm__tt
  7477.          Unify  a  subterm.    Note  this  may the  return  value  of  a
  7478.          PPLL__nneeww__tteerrmm__rreeff(())call to get access to a variable.
  7479.  
  7480.     PL_CHARS ccoonnsstt cchhaarr **
  7481.          Unify the argument  with an atom,  constructed from the C  char
  7482.          *, as in PPLL__uunniiffyy__aattoomm__cchhaarrss(()).
  7483.  
  7484.     PL_FUNCTOR ffuunnccttoorr__tt,, ......
  7485.          Unify the argument  with a compound  term.  This  specification
  7486.          should be  followed by  exactly as many  specifications as  the
  7487.          number of arguments of the compound term.
  7488.  
  7489.     PL_LIST iinntt lleennggtthh,, ......
  7490.          Create  a  list  of  the  indicated  length.     The  following
  7491.          arguments contain the elements of the list.
  7492.  
  7493.     For  example, to  unify an argument  with the term  language(dutch),
  7494.     the following skeleton may be used:
  7495.  
  7496.     static functor_t FUNCTOR_language1;
  7497.  
  7498.     static void
  7499.     init_constants()
  7500.     { FUNCTOR_language1 = PL_new_functor(PL_new_atom("language"), 1);
  7501.     }
  7502.  
  7503.     foreign_t
  7504.     pl_get_lang(term_t r)
  7505.     { return PL_unify_term(r,
  7506.                            PL_FUNCTOR, FUNCTOR_language1,
  7507.                                PL_CHARS, "dutch");
  7508.     }
  7509.  
  7510.     install_t
  7511.     install()
  7512.     { PL_register_foreign("get_lang", 1, pl_get_lang, 0);
  7513.       init_constants();
  7514.     }
  7515.  
  7516.  
  7517. 55..66..66 CCaalllliinngg PPrroolloogg ffrroomm CC
  7518.  
  7519. The Prolog  engine can  be called from  C. There  are to interfaces  for
  7520. this.    For the  first, a  term is  created that  could be  used as  an
  7521. argument to  ccaallll//11 and  next PPLL__ccaallll(()) is used  to call Prolog.    This
  7522. system is simple,  but does not allow  to inspect the different  answers
  7523. to  a non-deterministic  goal  and is  relatively  slow as  the  runtime
  7524. system needs  to find the predicate.   The  other interface is based  on
  7525. PPLL__ooppeenn__qquueerryy(()), PPLL__nneexxtt__ssoolluuttiioonn(())and  PPLL__ccuutt__qquueerryy(())or PPLL__cclloossee__qquueerryy(()).
  7526. This mechanism is more powerful, but also more complicated to use.
  7527.  
  7528.  
  7529. 55..66..66..11 PPrreeddiiccaattee rreeffeerreenncceess
  7530.  
  7531. This  section  discusses   the  functions  used  to  communicate   about
  7532. predicates.   Though a Prolog predicate  may defined or not,  redefined,
  7533. etc., a Prolog predicate has a handle that is  not destroyed, nor moved.
  7534. This handle is known by the type predicate_t.
  7535.  
  7536.  
  7537. _p_r_e_d_i_c_a_t_e___t PPLL__pprreedd((_f_u_n_c_t_o_r___t _f_, _m_o_d_u_l_e___t _m))
  7538.     Return  a handle to a predicate for the specified name/arity  in the
  7539.     given module.   This function always succeeds, creating a handle for
  7540.     an undefined predicate if no handle was available.
  7541.  
  7542.  
  7543. _p_r_e_d_i_c_a_t_e___t PPLL__pprreeddiiccaattee((_c_o_n_s_t _c_h_a_r _*_n_a_m_e_, _i_n_t _a_r_i_t_y_, _c_o_n_s_t _c_h_a_r_* _m_o_d_u_l_e))
  7544.  
  7545.     Same   a  PPLL__pprreedd(()),   but  provides  a  more  convenient   interface
  7546.     to the C-programmer.
  7547.  
  7548.  
  7549. _v_o_i_d PPLL__pprreeddiiccaattee__iinnffoo((_p_r_e_d_i_c_a_t_e___t _p_, _a_t_o_m___t _*_n_, _i_n_t _*_a_, _m_o_d_u_l_e___t _*_m))
  7550.     Return  information on the  predicate _p.   The  name is stored  over
  7551.     _n,  the  arity  over _a,  while  _m  receives the  definition  module.
  7552.     Note  that  the  latter need  not  be  the same  as  specified  with
  7553.     PPLL__pprreeddiiccaattee(()).    If the  predicate was  imported  into the  module
  7554.     given  to PPLL__pprreeddiiccaattee(()), this function will return the  module where
  7555.     the predicate was defined.
  7556.  
  7557.  
  7558. 55..66..66..22 IInniittiiaattiinngg aa qquueerryy ffrroomm CC
  7559.  
  7560. This  section discusses  the  functions  for creating  and  manipulating
  7561. queries from C. Note that a foreign context can have  at most one active
  7562. query.    This  implies it  is allowed  to  make strictly  nested  calls
  7563. between  C and  Prolog (Prolog  calls C,  calls Prolog,  calls C,  etc.,
  7564. but it  is nnoott  allowed to  open multiple queries  and start  generating
  7565. solutions for  each of them  by calling PPLL__nneexxtt__ssoolluuttiioonn(()).   Be sure  to
  7566. call PPLL__ccuutt__qquueerryy(()) or PPLL__cclloossee__qquueerryy(())on  any query  you opened  before
  7567. opening the next or returning control back to Prolog.
  7568.  
  7569.  
  7570. _q_i_d___t PPLL__ooppeenn__qquueerryy((_m_o_d_u_l_e___t _c_t_x_, _i_n_t _f_l_a_g_s_, _p_r_e_d_i_c_a_t_e___t _p_, _t_e_r_m___t _+_t_0))
  7571.  
  7572.     Opens  a query  and returns  an identifier for  it.   This  function
  7573.     always  succeeds,  regardless whether  the predicate  is defined  or
  7574.     not.    _c_t_x is  the _c_o_n_t_e_x_t  _m_o_d_u_l_e of  the goal.    When NULL,  the
  7575.     context  module of  the calling  context will  be used,  or user  if
  7576.     there  is no calling  context (as may  happen in embedded  systems).
  7577.     Note  that the  context module  only matters  for _m_o_d_u_l_e___t_r_a_n_s_p_a_r_e_n_t
  7578.     predicates.   See ccoonntteexxtt__mmoodduullee//11 and mmoodduullee__ttrraannssppaarreenntt//11.  The  _p
  7579.     argument  specifies the  predicate, and  should be the  result of  a
  7580.     call  to PPLL__pprreedd(()) or PPLL__pprreeddiiccaattee(()).   Note that  it is allowed  to
  7581.     store  this handle as global data  and reuse it for future  queries.
  7582.     The  term-reference _t_0 is the  first of a vector of  term-references
  7583.     as returned by PPLL__nneeww__tteerrmm__rreeffss((_n)).
  7584.  
  7585.     The  _f_l_a_g_s  arguments provides  some additional  options  concerning
  7586.     debugging  and  exception handling.    It  is a  bitwise or  of  the
  7587.     following values:
  7588.  
  7589.     PL_Q_NORMAL
  7590.          Normal operation.    The  debugger inherits  its settings  from
  7591.          the environment.   If an exception  occurs that is not  handled
  7592.          in Prolog, a  message is printed and  the tracer is started  to
  7593.          debug the error.
  7594.  
  7595.     PL_Q_NODEBUG
  7596.          Switch off the debugger while executing the goal.   This option
  7597.          is used by many  calls to hook-predicates to avoid  tracing the
  7598.          hooks.   An example is  pprriinntt//11 calling ppoorrttrraayy//11 from  foreign
  7599.          code.
  7600.  
  7601.     PL_Q_CATCH_EXCEPTION
  7602.          If an  exception is  raised while  executing the  goal, do  not
  7603.          report it, but make it available for PPLL__eexxcceeppttiioonn(()).
  7604.  
  7605.     PL_Q_PASS_EXCEPTION
  7606.          As PL_Q_CATCH_EXCEPTION,  but do  not invalidate the  exception-
  7607.          term  while   calling  PPLL__cclloossee__qquueerryy(()).       This  option   is
  7608.          experimental.
  7609.  
  7610.     The  example below opens a query to the predicate is_a/2  to find the
  7611.     ancestor of for some name.
  7612.  
  7613.     char *
  7614.     ancestor(const char *me)
  7615.     { term_t a0 = PL_new_term_refs(2);
  7616.       static predicate_t p;
  7617.  
  7618.       if ( !p )
  7619.         p = PL_predicate("is_a", 2, "database");
  7620.  
  7621.       PL_put_atom_chars(a0, me);
  7622.       PL_open_query(NULL, PL_Q_NORMAL, p, a0);
  7623.       ...
  7624.     }
  7625.  
  7626.  
  7627. _i_n_t PPLL__nneexxtt__ssoolluuttiioonn((_q_i_d___t _q_i_d))
  7628.     Generate the first (next)  solution for the given query.  The return
  7629.     value  is TRUE if  a solution  was found, or  FALSE to indicate  the
  7630.     query  could not be proven.  This function may be  called repeatedly
  7631.     until it fails to generate all solutions to the query.
  7632.  
  7633.  
  7634. _v_o_i_d PPLL__ccuutt__qquueerryy((_q_i_d))
  7635.     Discards  the query, but does not delete any of the data  created by
  7636.     the  query.   It just  invalidate _q_i_d,  allowing for a  new call  to
  7637.     PPLL__ooppeenn__qquueerryy(())in this context.
  7638.  
  7639.  
  7640. _v_o_i_d PPLL__cclloossee__qquueerryy((_q_i_d))
  7641.     As  PPLL__ccuutt__qquueerryy(()),  but all  data and bindings  created by the  query
  7642.     are destroyed.
  7643.  
  7644.  
  7645. _i_n_t PPLL__ccaallll__pprreeddiiccaattee((_m_o_d_u_l_e___t _m_, _i_n_t _d_e_b_u_g_, _p_r_e_d_i_c_a_t_e___t _p_r_e_d_, _t_e_r_m___t _+_t_0))
  7646.  
  7647.     Shorthand   for  PPLL__ooppeenn__qquueerryy(()),   PPLL__nneexxtt__ssoolluuttiioonn(()),  PPLL__ccuutt__qquueerryy(()),
  7648.     generating  a single solution.   The arguments  are the same as  for
  7649.     PPLL__ooppeenn__qquueerryy(()), the return value is the same as PPLL__nneexxtt__ssoolluuttiioonn(()).
  7650.  
  7651.  
  7652. _i_n_t PPLL__ccaallll((_t_e_r_m___t_, _m_o_d_u_l_e___t))
  7653.     Call term just like  the Prolog predicate oonnccee//11.  _T_e_r_m is called in
  7654.     the  specified module, or in the context  module if module_t  = NULL.
  7655.     Returns  TRUE if  the call succeeds,  FALSE otherwise.   Figure  5.4
  7656.     shows an example to  obtain the number of defined atoms.  All checks
  7657.     are omitted to improve readability.
  7658.  
  7659.  
  7660. 55..66..77 DDiissccaarrddiinngg DDaattaa
  7661.  
  7662. The Prolog  data created and  term-references needed  to setup the  call
  7663. and/or analyse  the result can  in most cases  be discarded right  after
  7664. the  call.    PPLL__cclloossee__qquueerryy(())allows  for destructing  the  data,  while
  7665. leaving the  term-references.   The calls below may  be used to  destroy
  7666. term-references and data.  See figure 5.4 for an example.
  7667.  
  7668.  
  7669. _f_i_d___t PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(())
  7670.     Created  a foreign  frame,  holding a  mark that  allows the  system
  7671.     to  undo  bindings and  destroy data  created after  it  as well  as
  7672.     providing  the  environment  for  creating term-references.     This
  7673.     function   is  called  by  the  kernel  before  calling   a  foreign
  7674.     predicate.
  7675.  
  7676.  
  7677. _v_o_i_d PPLL__cclloossee__ffoorreeiiggnn__ffrraammee((_f_i_d___t _i_d))
  7678.     Discard  all term-references  created  after the  frame was  opened.
  7679.     All  other Prolog data is retained.  This function is called  by the
  7680.     kernel whenever a foreign function returns control back to Prolog.
  7681.  
  7682.  
  7683. _v_o_i_d PPLL__ddiissccaarrdd__ffoorreeiiggnn__ffrraammee((_f_i_d___t _i_d))
  7684.     Same  as PPLL__cclloossee__ffoorreeiiggnn__ffrraammee(()), but  also undo  all bindings  made
  7685.     since the open and destroy all Prolog data.
  7686.  
  7687. It is obligatory to call either of the two  closing functions to discard
  7688. a foreign frame.  Foreign frames may be nested.
  7689.  
  7690. int
  7691. count_atoms()
  7692. { fid_t fid = PL_open_foreign_frame();
  7693.   term_t goal  = PL_new_term_ref();
  7694.   term_t a1    = PL_new_term_ref();
  7695.   term_t a2    = PL_new_term_ref();
  7696.   functor_t s2 = PL_new_functor(PL_new_atom("statistics"), 2);
  7697.  
  7698.   int atoms;
  7699.  
  7700.   PL_put_atom_chars(a1, "atoms");
  7701.   PL_cons_functor(goal, s2, a1, a2);
  7702.   PL_call(goal, NULL);         /* call it in current module */
  7703.  
  7704.   PL_get_integer(a2, &atoms);
  7705.   PL_discard_foreign_frame(fid);
  7706.  
  7707.   return atoms;
  7708. }
  7709.  
  7710.                       Figure 5.4:  Calling Prolog
  7711.  
  7712.  
  7713. 55..66..88 FFoorreeiiggnn CCooddee aanndd MMoodduulleess
  7714.  
  7715. Modules are  identified via a  unique handle.   The following  functions
  7716. are available to query and manipulate modules.
  7717.  
  7718.  
  7719. _m_o_d_u_l_e___t PPLL__ccoonntteexxtt(())
  7720.     Return the module  identifier of the context module of the currently
  7721.     active foreign predicate.
  7722.  
  7723.  
  7724. _i_n_t PPLL__ssttrriipp__mmoodduullee((_t_e_r_m___t _+_r_a_w_, _m_o_d_u_l_e___t _*_m_, _t_e_r_m___t _-_p_l_a_i_n))
  7725.     Utility  function.  If  _r_a_w is a  term, possibly holding the  module
  7726.     construct  <_m_o_d_u_l_e>:<_r_e_s_t>this function will make _p_l_a_i_n a  reference
  7727.     to  <_r_e_s_t>  and fill  _m_o_d_u_l_e _*  with <_m_o_d_u_l_e>.    For further  nested
  7728.     module  constructs  the inner  most module  is  returned via  _m_o_d_u_l_e
  7729.     _*.   If  _r_a_w is  not a module  construct _a_r_g will  simply be put  in
  7730.     _p_l_a_i_n.   If _m_o_d_u_l_e _* is NULL  it will be set to the  context module.
  7731.     Otherwise  it will be left untouched.   The following example  shows
  7732.     how  to obtain the  plain term and module  if the default module  is
  7733.     the user module:
  7734.  
  7735.     { module m = PL_new_module(PL_new_atom("user"));
  7736.       term_t plain = PL_new_term_ref();
  7737.  
  7738.       PL_strip_module(term, &m, plain);
  7739.       ...
  7740.  
  7741.  
  7742. _a_t_o_m___t PPLL__mmoodduullee__nnaammee((_m_o_d_u_l_e___t))
  7743.     Return the name of _m_o_d_u_l_e as an atom.
  7744.  
  7745.  
  7746. _m_o_d_u_l_e___t PPLL__nneeww__mmoodduullee((_a_t_o_m___t _n_a_m_e))
  7747.     Find  an existing or create a new module with name specified  by the
  7748.     atom _n_a_m_e.
  7749.  
  7750.  
  7751. 55..66..99 PPrroolloogg eexxcceeppttiioonnss iinn ffoorreeiiggnn ccooddee
  7752.  
  7753. This  section  discusses PPLL__eexxcceeppttiioonn(())  and PPLL__tthhrrooww(()),  the  interface
  7754. functions  to  detect  and  generate  Prolog   exceptions  from  C-code.
  7755. PPLL__tthhrrooww(())is similar to tthhrrooww//11, and  may be used to return an exception
  7756. from  a foreign  predicate.    After  calling  PPLL__tthhrrooww(()),  the  function
  7757. implementing a foreign predicate  should return failure.  If  success is
  7758. returned, the exception is simply discarded.  Calling PPLL__tthhrrooww(()) outside
  7759. the context  of a function implementing  a foreign predicate results  in
  7760. undefined behaviour.
  7761.  
  7762. PPLL__eexxcceeppttiioonn(()) may be used after a call to  PPLL__nneexxtt__ssoolluuttiioonn(())fails,  and
  7763. returns  a term  reference to  an  exception term  if an  exception  was
  7764. raised, and 0 otherwise.
  7765.  
  7766. If a  C-function, implementing a predicate  calls Prolog and detects  an
  7767. exception using PPLL__eexxcceeppttiioonn(()), it can handle this exception,  or return
  7768. with  the exception.    Some caution  is required  though.    It is  nnoott
  7769. allowed to call PPLL__cclloossee__qquueerryy(())or PPLL__ddiissccaarrdd__ffoorreeiiggnn__ffrraammee(())afterwards,
  7770. as this  will invalidate the  exception term.   Below  is the code  that
  7771. calls a Prolog defined arithmetic function (see aarriitthhmmeetthhiicc__ffuunnccttiioonn//11).
  7772.  
  7773. If PPLL__nneexxtt__ssoolluuttiioonn(()) succeeds, the  result is  analysed and  translated
  7774. to  a number,  after  which the  query  is closed  and all  Prolog  data
  7775. created after  PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(())is destroyed.   On the other  hand,
  7776. if PPLL__nneexxtt__ssoolluuttiioonn(())fails  and if  an exception was  raised, just  pass
  7777. it.   Otherwise  generate an exception  (PPLL__eerrrroorr(()) is an internal  call
  7778. for building  the standard error  terms and calling PPLL__tthhrrooww(())).   After
  7779. this,  the Prolog  environment should  be discarded using  PPLL__ccuutt__qquueerryy(())
  7780. and PPLL__cclloossee__ffoorreeiiggnn__ffrraammee(())to avoid invalidating the exception term.
  7781.  
  7782. static int
  7783. prologFunction(ArithFunction f, term_t av, Number r)
  7784. { int arity = f->proc->definition->functor->arity;
  7785.   fid_t fid = PL_open_foreign_frame();
  7786.   qid_t qid;
  7787.   int rval;
  7788.  
  7789.   qid = PL_open_query(NULL, PL_Q_NORMAL, f->proc, av);
  7790.  
  7791.   if ( PL_next_solution(qid) )
  7792.   { rval = valueExpression(av+arity-1, r);
  7793.     PL_close_query(qid);
  7794.     PL_discard_foreign_frame(fid);
  7795.   } else
  7796.   { term_t except;
  7797.  
  7798.     if ( (except = PL_exception(qid)) )
  7799.     { rval = PL_throw(except);          /* pass exception */
  7800.     } else
  7801.     { char *name = stringAtom(f->proc->definition->functor->name);
  7802.  
  7803.                                         /* generate exception */
  7804.       rval = PL_error(name, arity-1, NULL, ERR_FAILED, f->proc);
  7805.     }
  7806.  
  7807.     PL_cut_query(qid);                  /* donot destroy data */
  7808.     PL_close_foreign_frame(fid);        /* same */
  7809.   }
  7810.  
  7811.   return rval;
  7812. }
  7813.  
  7814.  
  7815. _i_n_t PPLL__tthhrrooww((_t_e_r_m___t _e_x_c_e_p_t_i_o_n))
  7816.     Generate  an exception (as  tthhrrooww//11) and return  FALSE. Below is  an
  7817.     example returning an exception from foreign predicate:
  7818.  
  7819.     foreign_t
  7820.     pl_hello(term_t to)
  7821.     { char *s;
  7822.  
  7823.       if ( PL_get_atom_chars(to, &s) )
  7824.       { Sprintf("Hello \"%s\"\n", s);
  7825.  
  7826.         PL_succeed;
  7827.       } else
  7828.       { term_t except = PL_new_term_ref();
  7829.  
  7830.         PL_unify_term(except,
  7831.                       PL_FUNCTOR, PL_new_functor(PL_new_atom("type_error"), 2),
  7832.                         PL_ATOM,    "atom",
  7833.                         PL_TERM,    to);
  7834.  
  7835.         return PL_throw(except);
  7836.       }
  7837.     }
  7838.  
  7839.  
  7840. _t_e_r_m___t PPLL__eexxcceeppttiioonn((_q_i_d___t _q_i_d))
  7841.     If  PPLL__nneexxtt__ssoolluuttiioonn(()) fails, this  can  be  due to  normal  failure
  7842.     of  the  Prolog  call, or  because  an  exception was  raised  using
  7843.     tthhrrooww//11.   This function returns  a handle to the exception term  if
  7844.     an exception was raised, or 0 if the Prolog goal simply failed..
  7845.  
  7846.  
  7847. 55..66..1100 MMiisscceellllaanneeoouuss
  7848.  
  7849.  
  7850. _i_n_t PPLL__ccoommppaarree((_t_e_r_m___t _t_1_, _t_e_r_m___t _t_2))
  7851.     Compares  two terms using  the standard order  of terms and  returns
  7852.     -1, 0 or 1.  See also ccoommppaarree//33.
  7853.  
  7854.  
  7855. 55..66..1111 CCaattcchhiinngg SSiiggnnaallss ((SSooffttwwaarree IInntteerrrruuppttss))
  7856.  
  7857. SWI-Prolog  catches the  Unix  signals SIGINT,  SIGFPE and  SIGSEGV.  To
  7858. avoid  problems with  foreign  code attempting  to catch  these  signals
  7859. foreign code  should call PPLL__ssiiggnnaall(()) to install signal handlers  rather
  7860. than the Unix library function signal().  SWI-Prolog  will always handle
  7861. SIGINT  itself.   SIGFPE  and SIGSEGV  are passed  to  the foreign  code
  7862. handlers if Prolog did not expect that signal.
  7863.  
  7864.  
  7865. _v_o_i_d _(_*_)_(_) PPLL__ssiiggnnaall((_s_i_g_, _f_u_n_c))
  7866.     This  function  should be  used to  install  signal handlers  rather
  7867.     than  the Unix  library function  signal().   It ensures  consistent
  7868.     signal  handling  between   SWI-Prolog  and  the  foreign  code  and
  7869.     reinstalls  signal handlers  if a state  created with ssaavvee__pprrooggrraamm//11
  7870.     is restarted.
  7871.  
  7872.  
  7873. 55..66..1122 EErrrroorrss aanndd wwaarrnniinnggss
  7874.  
  7875. Two standard functions are available to print standard  Prolog errors to
  7876. the standard error stream.
  7877.  
  7878.  
  7879. _i_n_t PPLL__wwaarrnniinngg((_f_o_r_m_a_t_, _a_1_, _._._.))
  7880.     Print  an  error message  starting  with `[WARNING: ',  followed  by
  7881.     the  output from  _f_o_r_m_a_t, followed  by a `]'  and a newline.    Then
  7882.     start  the tracer.   _f_o_r_m_a_t and  the arguments are  the same as  for
  7883.     pprriinnttff((2)).  Always returns FALSE.
  7884.  
  7885.  
  7886. 55..66..1133 EEnnvviirroonnmmeenntt CCoonnttrrooll ffrroomm FFoorreeiiggnn CCooddee
  7887.  
  7888.  
  7889. _i_n_t PPLL__aaccttiioonn((_i_n_t_, _C___t_y_p_e))
  7890.     Perform  some  action on  the  Prolog system.    _i_n_t  describes  the
  7891.     action,  _C___t_y_p_e provides the argument if necessary.   The actions are
  7892.     listed in table 5.1.
  7893.     ___________________________________________________________________
  7894.     | PL_ACTION_TRACE        |Start Prolog tracer                      |
  7895.     | PL_ACTION_DEBUG        |Switch on Prolog debug mode              |
  7896.     | PL_ACTION_BACKTRACE    |Print   backtrace   on   current   output|
  7897.     |                        |stream.   The  argument (an  int) is  the|
  7898.     |                        |number of frames printed.                |
  7899.     | PL_ACTION_HALT         |Halt  Prolog  execution.     This  action|
  7900.  
  7901.     |                        |should be called rather  than Unix exit()|
  7902.     |                        |to give  Prolog the opportunity  to clean|
  7903.     |                        |up.  This call does not return.          |
  7904.     | PL_ACTION_ABORT        |Generate a Prolog abort.   This call does|
  7905.     |                        |not return.                              |
  7906.     | PL_ACTION_BREAK        |Create a  standard Prolog  break environ-|
  7907.     |                        |ment.     Returns after  the  user  types|
  7908.     |                        |control-D.                               |
  7909.  
  7910.     | PL_ACTION_SYMBOLFILE   |The  argument (a  char  *) is  considered|
  7911.     |                        |to  be hold  the  symbolfile for  further|
  7912.     |                        |incremental   loading.        Should   be|
  7913.     |                        |called by user  applications that perform|
  7914.     |                        |incremental loading  as well and  want to|
  7915.     |________________________|inform_Prolog_of_the_new_symbol_table.___|
  7916.  
  7917.                      Table 5.1:  PPLL__aaccttiioonn(())options
  7918.  
  7919.  
  7920. 55..66..1144 QQuueerryyiinngg PPrroolloogg
  7921.  
  7922.  
  7923. _C___t_y_p_e PPLL__qquueerryy((_i_n_t))
  7924.     Obtain  status  information  on  the  Prolog system.     The  actual
  7925.     argument  type depends on the  information required.  _i_n_t  describes
  7926.     what information is wanted.  The options are given in table 5.2.
  7927.     ___________________________________________________________________
  7928.     | PL_QUERY_ARGC          |Return an  integer holding the  number of|
  7929.     |                        |arguments given to Prolog from Unix.     |
  7930.     | PL_QUERY_ARGV          |Return  a char  **  holding the  argument|
  7931.     |                        |vector given to Prolog from Unix.        |
  7932.     | PL_QUERY_SYMBOLFILE    |Return  a  char  *  holding  the  current|
  7933.  
  7934.     |                        |symbol file of the running process.      |
  7935.     | PL_QUERY_ORGSYMBOLFILE |Return  the initial  symbol file  (before|
  7936.     |                        |loading)  of  Prolog.    By  setting  the|
  7937.     |                        |symbol  file   to  this  value   no  name|
  7938.     |                        |clashes can occur  with previously loaded|
  7939.     |                        |foreign  files  (but no  symbols  can  be|
  7940.     |                        |shared  with  earlier loaded  modules  as|
  7941.     |                        |well).                                   |
  7942.  
  7943.     | PL_MAX_INTEGER         |Return a  long, representing  the maximal|
  7944.     |                        |integer  value  represented  by  Prolog's|
  7945.     |                        |tagged integers.                         |
  7946.     | PL_MIN_INTEGER         |Return  a long,  represented the  minimal|
  7947.     |                        |integer value.                           |
  7948.     | PL_QUERY_VERSION       |Return a  long, representing  the version|
  7949.  
  7950.     |                        |as 10; 000M* +100m* +p, where  M is  the |
  7951.     |                        |major,  m the  minor version  number and |
  7952.     |                        |p the  patch-level.   For  example, 20717|
  7953.     |________________________|means_2.7.17.____________________________|
  7954.  
  7955.                      Table 5.2:  PPLL__qquueerryy(()) options
  7956.  
  7957.  
  7958. 55..66..1155 RReeggiisstteerriinngg FFoorreeiiggnn PPrreeddiiccaatteess
  7959.  
  7960.  
  7961. _i_n_t PPLL__rreeggiisstteerr__ffoorreeiiggnn((_n_a_m_e_, _a_r_i_t_y_, _f_u_n_c_t_i_o_n_, _f_l_a_g_s))
  7962.     Register  a C-function to implement a Prolog predicate.   After this
  7963.     call returns successfully  a predicate with name _n_a_m_e (a char *) and
  7964.     arity  _a_r_i_t_y (a C int)  is created.   When called in Prolog,  Prolog
  7965.     will  call _f_u_n_c_t_i_o_n.  _f_l_a_g_s forms bitwise or'ed list of  options for
  7966.     the installation.  These are:
  7967.     ___________________________________________________________________
  7968.     | PL_FA_NOTRACE          |Predicate cannot be seen in the tracer   |
  7969.     | PL_FA_TRANSPARENT      |Predicate is module transparent          |
  7970.     | PL_FA_NONDETERMINISTIC |Predicate  is  non-deterministic.     See|
  7971.     |________________________|also_PPLL__rreettrryy(())._________________________|
  7972.  
  7973.  
  7974. _v_o_i_d PPLL__rreeggiisstteerr__eexxtteennssiioonnss((_P_L___e_x_t_e_n_s_i_o_n _*_e))
  7975.     Register  foreign predicates from a table  of structures.  The  type
  7976.     PL_extension is defined as:
  7977.  
  7978.     typedef struct _PL_extension
  7979.     { char          *predicate_name; /* Name of the predicate */
  7980.       short         arity;           /* Arity of the predicate */
  7981.       pl_function_t function;        /* Implementing functions */
  7982.       short         flags;           /* Or of PL_FA_... */
  7983.     } PL_extension;
  7984.  
  7985.     Here is an example of its usage:
  7986.  
  7987.     static PL_extension predicates[] = {
  7988.     { "foo",        1,      pl_foo, 0 },
  7989.     { "bar",        2,      pl_bar, PL_FA_NONDETERMINISTIC },
  7990.     { NULL,         0,      NULL,   0 }
  7991.     };
  7992.  
  7993.     main(int argc, char **argv)
  7994.     { PL_register_extensions(predicates);
  7995.  
  7996.       if ( !PL_initialise(argc, argv) )
  7997.         PL_halt(1);
  7998.  
  7999.       ...
  8000.     }
  8001.  
  8002.     The  function PPLL__rreeggiisstteerr__eexxtteennssiioonnss(())is the only PL_*  function that
  8003.     may  be called bbeeffoorree PPLL__iinniittiiaalliissee(()).  The  functions are registered
  8004.     after  registration  of the  SWI-Prolog builtin  foreign  predicates
  8005.     and  before loading  the initial  saved state.    This implies  that
  8006.     iinniittiiaalliizzaattiioonn//11 directives can refer to them.
  8007.  
  8008.  
  8009. 55..66..1166 FFoorreeiiggnn CCooddee HHooookkss
  8010.  
  8011. For various specific applications some hooks re provided.
  8012.  
  8013.  
  8014. _P_L___d_i_s_p_a_t_c_h___h_o_o_k___t PPLL__ddiissppaattcchh__hhooookk((_P_L___d_i_s_p_a_t_c_h___h_o_o_k___t))
  8015.     If  this hook  is not  NULL, this  function is  called when  reading
  8016.     from  the  terminal.     It is  supposed  to  dispatch  events  when
  8017.     SWI-Prolog  is connected  to a window  environment.   It can  return
  8018.     two  values:  PL_DISPATCH_INPUT indicates Prolog input is  available
  8019.     on  file descriptor 0 or PL_DISPATCH_TIMEOUT to indicate a  timeout.
  8020.     The  old hook is  returned.  The  type PL_dispatch_hook_t is  defined
  8021.     as:
  8022.  
  8023.     typedef int  (*PL_dispatch_hook_t)(void);
  8024.  
  8025.  
  8026. _v_o_i_d PPLL__aabboorrtt__hhooookk((_P_L___a_b_o_r_t___h_o_o_k___t))
  8027.     Install  a hook  when aabboorrtt//00 is  executed.   SWI-Prolog aabboorrtt//00  is
  8028.     implemented  using C  setjmp()/longjmp() construct.   The hooks  are
  8029.     executed  in  the  reverse order  of  their registration  after  the
  8030.     longjmp()  took place and before  the Prolog toplevel is  reinvoked.
  8031.     The type PL_abort_hook_t is defined as:
  8032.  
  8033.     typedef void (*PL_abort_hook_t)(void);
  8034.  
  8035.  
  8036. _i_n_t PPLL__aabboorrtt__uunnhhooookk((_P_L___a_b_o_r_t___h_o_o_k___t))
  8037.     Remove  a hook installed with  PPLL__aabboorrtt__hhooookk(()).   Returns FALSE if  no
  8038.     such hook is found, TRUE otherwise.
  8039.  
  8040.  
  8041. _v_o_i_d PPLL__rreeiinniitt__hhooookk((_P_L___r_e_i_n_i_t___h_o_o_k___t))
  8042.     Install  a  hook   that  is  called  when  a  saved  program  (using
  8043.     ssaavvee__pprrooggrraamm//[[11,,22]]) is restored.   The  hooks are called in  reverse
  8044.     order.  The type PL_reinit_hook_t is defined as:
  8045.  
  8046.     typedef void (*PL_reinit_hook_t)(int argc, char **argv);
  8047.  
  8048.  
  8049. _i_n_t PPLL__rreeiinniitt__uunnhhooookk((_P_L___r_e_i_n_i_t___h_o_o_k___t))
  8050.     Remove  a hook installed with PPLL__rreeiinniitt__hhooookk(()).  Returns FALSE  if no
  8051.     such hook is found, TRUE otherwise.
  8052.  
  8053.  
  8054. 55..66..1177 EEmmbbeeddddiinngg SSWWII--PPrroolloogg iinn aa CC--pprrooggrraamm
  8055.  
  8056. As  of  version  2.1.0,  SWI-Prolog may  be  embedded  in  a  C-program.
  8057. To  reach  at  a compiled  C-program  with  SWI-Prolog  as  an  embedded
  8058. application is very  similar to creating a statically linked  SWI-Prolog
  8059. executable as described in section 5.4.1.
  8060.  
  8061. The  file   .../pl/include/stub.c  defines   SWI-Prologs  default   main
  8062. program:
  8063.  
  8064. int
  8065. main(int argc, char **argv)
  8066. { if ( !PL_initialise(argc, argv) )
  8067.     PL_halt(1);
  8068.  
  8069.   PL_install_readline();        /* delete if you don't want readline */
  8070.  
  8071.   PL_halt(PL_toplevel() ? 0 : 1);
  8072. }
  8073.  
  8074. This  may be  replaced with  your own  main C-program.    The  interface
  8075. function  PPLL__iinniittiiaalliissee(()) mmuusstt  be   called  before  any  of  the  other
  8076. SWI-Prolog  foreign  language  functions  described  in   this  chapter.
  8077. PPLL__iinniittiiaalliissee(()) interprets all  the command-line  arguments, except  for
  8078. the -t toplevel flag that is interpreted by PPLL__ttoopplleevveell(()).
  8079.  
  8080.  
  8081. _i_n_t PPLL__iinniittiiaalliissee((_i_n_t _a_r_g_c_, _c_h_a_r _*_*_a_r_g_v_, _c_h_a_r _*_*_e_n_v_i_r_o_n))
  8082.     Initialises  the SWI-Prolog heap and  stacks, restores the boot  QLF
  8083.     file,  loads the system and personal initialisation files,  runs the
  8084.     aatt__iinniittiiaalliizzaattiioonn//11 hooks and finally runs the -g goal hook.
  8085.  
  8086.     PPLL__iinniittiiaalliissee(()) returns  1  if all  initialisation succeeded  and  0
  8087.     otherwise.    Various fatal errors  may cause  PL_initialise to  call
  8088.     PPLL__hhaalltt((_1)), preventing it from returning at all.
  8089.  
  8090.  
  8091. _v_o_i_d PPLL__iinnssttaallll__rreeaaddlliinnee(())
  8092.     Installs  the GNU-readline line-editor.  Embedded  applications that
  8093.     do  not use the  Prolog toplevel should  normally delete this  line,
  8094.     shrinking the Prolog kernel significantly.
  8095.  
  8096.  
  8097. _i_n_t PPLL__ttoopplleevveell(())
  8098.     Runs  the  goal of  the -t toplevel  switch  (default pprroolloogg//00)  and
  8099.     returns 1 if successful, 0 otherwise.
  8100.  
  8101.  
  8102. _v_o_i_d PPLL__hhaalltt((_i_n_t _s_t_a_t_u_s))
  8103.     Cleanup  the Prolog  environment and  calls exit()  with the  status
  8104.     argument.
  8105.  
  8106.  
  8107. 55..77 LLiinnkkiinngg eemmbbeeddddeedd aapppplliiccaattiioonnss uussiinngg pplllldd
  8108.  
  8109. The  utility program  plld (Win32:   plld.exe)  may be  used  to link  a
  8110. combination of C-files  and Prolog files into a stand-alone  executable.
  8111. plld automates most of what is described in the previous sections.
  8112.  
  8113. In  the  normal  usage,  a  copy  is   made  of  the  default  embedding
  8114. template  .../pl/include/stub.c.    The main()  routine is  modified  to
  8115. suit your application.   PPLL__iinniittiiaalliissee(())mmuusstt be passed  the program-name
  8116. (_a_r_g_v_[_0_])  (Win32:    the  executing  program  can   be  obtained  using
  8117. GGeettMMoodduulleeFFiilleeNNaammee(())).   The other  elements of  the command-line may  be
  8118. modified.  Next, plld is typically invoked as:
  8119.  
  8120. plld -o output stubfile.c [other-c-or-o-files] [plfiles]
  8121.  
  8122. plld  will first  split the  options into  various groups  for both  the
  8123. C-compiler and the Prolog  compiler.  Next, it will add  various default
  8124. options to the  C-compiler and call it  to create an executable  holding
  8125. the  user's C-code  and the  Prolog kernel.    Then,  it  will call  the
  8126. SWI-Prolog compiler  to create a  saved state  from the provided  Prolog
  8127. files  and finally,  it  will attach  this saved  state to  the  created
  8128. emulator to create the requested executable.
  8129.  
  8130. Below, it  is described how the options  are split and which  additional
  8131. options are passed.
  8132.  
  8133. --hheellpp
  8134.     Print brief synopsis.
  8135.  
  8136. --ppll _p_r_o_l_o_g
  8137.     Select  the prolog to use.   This prolog  is used for two  purposes:
  8138.     get  the home-directory as well  as the compiler/linker options  and
  8139.     create a saved state of the Prolog code.
  8140.  
  8141. --lldd _l_i_n_k_e_r
  8142.     Linker  used to  link the raw  executable.   Default is  to use  the
  8143.     C-compiler (Win32:  link.exe).
  8144.  
  8145. --cccc _C_-_c_o_m_p_i_l_e_r
  8146.     Compiler  for .c files  found on  the commandline.   Default is  the
  8147.     compiler used to build SWI-Prolog (see ffeeaattuurree//22) (Win32:  cl.exe).
  8148.  
  8149. --cc++++ _C_+_+_-_c_o_m_p_i_l_e_r
  8150.     Compiler  for C++ sources (extensions  .cpp, .cxx, .cc or .C)  files
  8151.     found  on the commandline.  Default is c++ or g++ if  the C-compiler
  8152.     is gcc) (Win32:  cl.exe).
  8153.  
  8154. --nnoossttaattee
  8155.     Just  relink  the  kernel,  do  not  add  any  Prolog  code  to  the
  8156.     new  kernel.     This  is  used  to  create  a  new  kernel  holding
  8157.     additional  foreign predicates on machines  that do not support  the
  8158.     shared-library  (DLL) interface, or if building the state  cannot be
  8159.     handled  by the default procedure used by plld.  In the  latter case
  8160.     the  state is created  seperately and appended  to the kernel  using
  8161.     cat <_k_e_r_n_e_l> <_s_t_a_t_e> > <_o_u_t>(Win32:  copy /b <_k_e_r_n_e_l>+<_s_t_a_t_e> <_o_u_t>)
  8162.  
  8163. --ppll--ooppttiioonnss _,_._._.
  8164.     Additional  options passed to Prolog when creating the  saved state.
  8165.     The  first character  immediately  following pl-options  is used  as
  8166.     separator  and  translated to  spaces when  the  argument is  built.
  8167.     Example:  -pl-options,-F,xpce  passed -F xpce as additional flags to
  8168.     Prolog.
  8169.  
  8170. --lldd--ooppttiioonnss _,_._._.
  8171.     Passes options to the linker, similar to -pl-options.
  8172.  
  8173. --cccc--ooppttiioonnss _,_._._.
  8174.     Passes options to the C/C++ compiler, similar to -pl-options.
  8175.  
  8176. --vv
  8177.     Select  verbose operation,  showing the  various programs and  their
  8178.     options.
  8179.  
  8180. --oo _o_u_t_f_i_l_e
  8181.     Reserved to specify the final output file.
  8182.  
  8183. --ll_l_i_b_r_a_r_y
  8184.     Specifies  a library for the C-compiler.   By default, -lpl  (Win32:
  8185.     libpl.lib) and the libraries needed by the Prolog kernel are given.
  8186.  
  8187. --LL_l_i_b_r_a_r_y_-_d_i_r_e_c_t_o_r_y
  8188.     Specifies  a  library directory  for  the C-compiler.    By  default
  8189.     the  directory  containing  the  Prolog C-library  for  the  current
  8190.     architecture is passed.
  8191.  
  8192. -g | -Iinclude-directory | -Ddefinition
  8193.     These  options  are passed  to  the C-compiler.    By  default,  the
  8194.     include directory containing  SWI-Prolog.h is passed.  plld adds two
  8195.     additional * -Ddef flags:
  8196.  
  8197.     --DD____SWI_PROLOG__
  8198.          Indicates the code is to be connected to SWI-Prolog.
  8199.  
  8200.     --DD____SWI_EMBEDDED__
  8201.          Indicates the creation of an embedded program.
  8202.  
  8203.  _*_._o | _*_._c | _*_._C | _*_._c_x_x | _*_._c_p_p
  8204.     Passed as input files to the C-compiler
  8205.  
  8206.  _*_._p_l |_*_._q_l_f
  8207.     Passed  as  input  files  to  the  Prolog  compiler  to  create  the
  8208.     saved-state.
  8209.  
  8210.  *
  8211.     I.e.  all other options.  These are passed as linker options  to the
  8212.     C-compiler.
  8213.  
  8214.  
  8215. 55..77..11 AA ssiimmppllee eexxaammppllee
  8216.  
  8217. The  following is  a very  simple example  going through  all the  steps
  8218. outlined above.   It  provides an arithmetic expression  evaluator.   We
  8219. will call  the application calc  and define it in  the files calc.c  and
  8220. calc.pl.  The Prolog file is simple:
  8221.  
  8222. calc(Atom) :-
  8223.         term_to_atom(Expr, Atom),
  8224.         A is Expr,
  8225.         write(A),
  8226.         nl.
  8227.  
  8228. The  C-part  of   the  application  parses  the  command-line   options,
  8229. initialises the  Prolog engine, locates  the calc/1 predicate and  calls
  8230. it.  The coder is in figure 5.5.
  8231.  
  8232. #include <stdio.h>
  8233. #include <SWI-Prolog.h>
  8234.  
  8235. PL_extension PL_extensions [] =
  8236. {
  8237.  
  8238. /*{ "name",     arity,  function,       PL_FA_<flags> },*/
  8239.  
  8240.   { NULL,       0,      NULL,           0 }     /* terminating line */
  8241. };
  8242.  
  8243. #define MAXLINE 1024
  8244.  
  8245. int
  8246. main(int argc, char **argv)
  8247. { char expression[MAXLINE];
  8248.   char *e = expression;
  8249.   char *program = argv[0];
  8250.   char *plav[2];
  8251.   int n;
  8252.  
  8253.   /* combine all the arguments in a single string */
  8254.  
  8255.   for(n=1; n<argc; n++)
  8256.   { if ( n != 1 )
  8257.       *e++ = ' ';
  8258.     strcpy(e, argv[n]);
  8259.     e += strlen(e);
  8260.  
  8261.   }
  8262.  
  8263.   /* make the argument vector for Prolog */
  8264.  
  8265.   plav[0] = program;
  8266.   plav[1] = NULL;
  8267.  
  8268.   /* initialise Prolog */
  8269.  
  8270.   if ( !PL_initialise(1, plav) )
  8271.     PL_halt(1);
  8272.  
  8273.   /* Lookup calc/1 and make the arguments and call */
  8274.  
  8275.   { predicate_t pred = PL_predicate("calc", 1, "user");
  8276.  
  8277.     term_t h0 = PL_new_term_refs(1);
  8278.     int rval;
  8279.  
  8280.     PL_put_atom_chars(h0, expression);
  8281.     rval = PL_call_predicate(NULL, FALSE, pred, h0);
  8282.  
  8283.     PL_halt(rval ? 0 : 1);
  8284.  
  8285.   }
  8286.  
  8287.   return 0;
  8288. }
  8289.  
  8290.              Figure 5.5:  C-source for the calc application
  8291.  
  8292. The application is now created using the following command-line:
  8293.  
  8294. % plld -o calc calc.c calc.pl
  8295.  
  8296. The following indicates the usage of the application:
  8297.  
  8298. % calc pi/2
  8299. 1.5708
  8300.  
  8301.  
  8302. 55..88 EExxaammppllee ooff UUssiinngg tthhee FFoorreeiiggnn IInntteerrffaaccee
  8303.  
  8304. Below is an example  showing all stages of the declaration of  a foreign
  8305. predicate that transforms atoms possibly holding  uppercase letters into
  8306. an atom only holding lower case letters.  Figure  5.6 shows the C-source
  8307. file, figure 5.7 illustrates compiling and loading of foreign code.
  8308. /*  Include file depends on local installation */
  8309.  
  8310. #include <SWI-Prolog.h>
  8311. #include <stdlib.h>
  8312. #include <ctype.h>
  8313.  
  8314. foreign_t
  8315. pl_lowercase(term_t u, term_t l)
  8316. { char *copy;
  8317.   char *s, *q;
  8318.  
  8319.   int rval;
  8320.  
  8321.   if ( !PL_get_atom_chars(u, &s) )
  8322.     return PL_warning("lowercase/2: instantiation fault");
  8323.   copy = malloc(strlen(s)+1);
  8324.  
  8325.   for( q=copy; *s; q++, s++)
  8326.  
  8327.     *q = (isupper(*s) ? tolower(*s) : *s);
  8328.   *q = '\0';
  8329.  
  8330.   rval = PL_unify_atom_chars(l, copy);
  8331.   free(copy);
  8332.  
  8333.   return rval;
  8334. }
  8335.  
  8336. install_t
  8337. install()
  8338. { PL_register_foreign("lowercase", 2, pl_lowercase, 0);
  8339. }
  8340.  
  8341.                    Figure 5.6:  Lowercase source file
  8342.  
  8343. % gcc -I/usr/local/lib/pl-\plversion/include -fpic -c lowercase.c
  8344. % gcc -shared -o lowercase.so lowercase.o
  8345.  
  8346. % pl
  8347. Welcome to SWI-Prolog (Version \plversion)
  8348. Copyright (c) 1993-1996 University of Amsterdam.  All rights reserved.
  8349.  
  8350. For help, use ?- help(Topic). or ?- apropos(Word).
  8351.  
  8352. 1 ?- load_foreign_library(lowercase).
  8353.  
  8354. Yes
  8355. 2 ?- lowercase('Hello World!', L).
  8356.  
  8357. L = 'hello world!'
  8358.  
  8359. Yes
  8360.  
  8361.     Figure 5.7:  Compiling the C-source and loading the object file
  8362.  
  8363.  
  8364. 55..99 NNootteess oonn UUssiinngg FFoorreeiiggnn CCooddee
  8365.  
  8366.  
  8367. 55..99..11 MMeemmoorryy AAllllooccaattiioonn
  8368.  
  8369. SWI-Prolog's  memory  allocation  is  based  on  the  mmaalllloocc((3))  library
  8370. routines.   Foreign  applications can safely  use mmaalllloocc((3)),  rreeaalllloocc((3))
  8371. and ffrreeee((3)).   Memory allocation using bbrrkk((2)) or ssbbrrkk((2)) is  not allowed
  8372. as these calls conflict with mmaalllloocc((3)).
  8373.  
  8374.  
  8375. 55..99..22 DDeebbuuggggiinngg FFoorreeiiggnn CCooddee
  8376.  
  8377. Statically  linked foreign  code  or embedded  systems can  be  debugged
  8378. normally.     Most  modern  environments  provide  debugging  tools  for
  8379. dynamically  loaded shared  objects  or dynamic  load  libraries.    The
  8380. following example traces  the code of lowercase  using ggddbb((1)) in a  Unix
  8381. environment.
  8382.  
  8383. % gcc -I/usr/local/lib/pl-2.2.0/include -fpic -c -g lowercase.c
  8384. % gcc -shared -o lowercase.so lowercase.o
  8385. % gdb pl
  8386. (gdb) r
  8387. Welcome to SWI-Prolog (Version \plversion)
  8388. Copyright (c) 1993-1996 University of Amsterdam.  All rights reserved.
  8389.  
  8390. For help, use ?- help(Topic). or ?- apropos(Word).
  8391.  
  8392. ?- load_foreign_library(lowercase).
  8393. <type Control-C>
  8394. (gdb) shared                    % loads symbols for shared objects
  8395. (gdb) break pl_lowercase
  8396. (gdb) continue
  8397. ?- lowercase('HELLO', X).
  8398.  
  8399.  
  8400. 55..99..33 NNaammee CCoonnfflliiccttss iinn CC mmoodduulleess
  8401.  
  8402. In  the  current  version  of the  system  all  public  C  functions  of
  8403. SWI-Prolog are in the symbol table.  This can lead  to name clashes with
  8404. foreign code.    Someday I  should write a  program to  strip all  these
  8405. symbols from the symbol table  (why does Unix not have that?).   For now
  8406. I can only suggest to give your function another name.   You can do this
  8407. using the C preprocessor.  If---for example---your  foreign package uses
  8408. a function warning(), which happens to exist in SWI-Prolog  as well, the
  8409. following macro should fix the problem.
  8410.  
  8411. #define warning warning_
  8412.  
  8413. Note  that shared  libraries do  not  have this  problem as  the  shared
  8414. library loader  will only look  for symbols in  the main executable  for
  8415. symbols that are not defined in the library itself.
  8416.  
  8417.  
  8418. 55..99..44 CCoommppaattiibbiilliittyy ooff tthhee FFoorreeiiggnn IInntteerrffaaccee
  8419.  
  8420. The term-reference  mechanism was first used  by Quintus Prolog  version
  8421. 3.     SICStus  Prolog version  3  is  strongly  based  on  the  Quintus
  8422. interface.  The  described SWI-Prolog interface is similar to  using the
  8423. Quintus or SICStus interfaces, defining all  foreign-predicate arguments
  8424. of  type  +term.    SWI-Prolog  explicitly uses  type  functor_t,  while
  8425. Quintus  and SICStus  uses <_n_a_m_e>  and  <_a_r_i_t_y>.   As  the  names of  the
  8426. functions differ  from Prolog to  Prolog, a  simple macro layer  dealing
  8427. with the names can also deal with this detail.  For example:
  8428.  
  8429. #define QP_put_functor(t, n, a) PL_put_functor(t, PL_new_functor(n, a))
  8430.  
  8431. The  PL_unify_*()  functions are  lacking from  the  Quintus and  SICStus
  8432. interface.    They can  easily  be emulated  or the  put/unify  approach
  8433. should be used to write compatible code.
  8434.  
  8435. The PPLL__ooppeenn__ffoorreeiiggnn__ffrraammee(())/PPLL__cclloossee__ffoorreeiiggnn__ffrraammee(())combination  is lacking
  8436. from both  other Prologs.   SICStus has  PPLL__nneeww__tteerrmm__rreeffss((_0)), followed  by
  8437. PPLL__rreesseett__tteerrmm__rreeffss(())that allows for discarding term references.
  8438.  
  8439. The  Prolog interface  for  the graphical  user interface  package  XPCE
  8440. shares about  90% of the code  using a simple  macro layer to deal  with
  8441. different naming and calling conventions of the interfaces.
  8442.  
  8443.  
  8444. CChhaapptteerr 66..  GGEENNEERRAATTIINNGG RRUUNNTTIIMMEE AAPPPPLLIICCAATTIIOONNSS
  8445.  
  8446. This  chapter  describes  the  features  of  SWI-Prolog  for  delivering
  8447. applications that can run without the development version  of the system
  8448. installed.
  8449.  
  8450. A SWI-Prolog  built application  consists of  at least two  parts:   the
  8451. emulator  and the  compiled application.    The latter  is  in the  same
  8452. format  as a  SWI-Prolog  boot-file  and SWI-Prolog  pre-compiled  (QLF)
  8453. file.   This  format is  fast loadable  and abstracted  just far  enough
  8454. to  be machine  independent.    This  implies an  application  delivered
  8455. in  binary format  can run  on any  computer for  which  an emulator  is
  8456. available without modification.
  8457.  
  8458.  
  8459. qqssaavvee__pprrooggrraamm((_+_F_i_l_e_, _+_L_i_s_t_O_f_O_p_t_i_o_n_s))
  8460.     Saves  the current  state of  the program  to the  file _F_i_l_e.    The
  8461.     result is an  executable shell-script, that will start the emulator.
  8462.     _L_i_s_t_O_f_O_p_t_i_o_n_s  is a  list of  <_K_e_y>=<_V_a_l_u_e> or  <_K_e_y>(<_V_a_l_u_e>)  pairs.
  8463.     The available keys are described in table 6.1.
  8464.  _______________________________________________________________________
  8465.  |__KKeeyy________________||OOppttiioonn__||________TTyyppee__________||DDeessccrriippttiioonn______________________________________________||__
  8466.  || local      | --LL   ||   K-bytes    |Size (Limit) of local stack       |
  8467.  | global     | --GG   ||   K-bytes    |Size (Limit) of global stack      |
  8468.  | trail      | --TT   ||   K-bytes    |Size (Limit) of trail stack       |
  8469.  | argument   | --AA   ||   K-bytes    |Size (Limit) of argument stack    |
  8470.  
  8471.  | goal       | --gg   ||     atom     |Initialisation goal               |
  8472.  | toplevel   | --tt   ||     atom     |Prolog toplevel goal              |
  8473.  |_init_file___|--ff___||_____atom_____|Personal_initialisation_file______|_
  8474.  | autoload   |      |     bool     |If true, run aauuttoollooaadd//00 first     |
  8475.  | map        |      |     atom     |File containing info on dump      |
  8476.  | op         |      |save/standard |Save operator declarations?       |
  8477.  |_stand_alone_|_____|_____bool_____|Include_the_emulator_in_the_state_|_
  8478.  
  8479.           Table 6.1:  <_K_e_y> = <_V_a_l_u_e> pairs for qqssaavvee__pprrooggrraamm//22
  8480.  
  8481.     The /bin/sh script contains the following data:
  8482.  
  8483.      1.  The _s_h_e_l_l _s_c_r_i_p_t _h_e_a_d_e_r starts as:
  8484.  
  8485.          #!/bin/sh
  8486.          #SAVE-VERSION=<num>
  8487.          #PROLOG-VERSION=<num>
  8488.          exec ${SWIPL-/path-to-emulator} -x $0 "$@"
  8489.  
  8490.      2.  The  _s_e_t_t_i_n_g_s  _s_e_c_t_i_o_n contains  the  default  values  for  the
  8491.          various command line options.
  8492.  
  8493.      3.  The  _p_r_e_d_i_c_a_t_e_s  _s_e_c_t_i_o_n  contains  all  predicates   from  the
  8494.          currently running  system.   Clauses of  predicates defined  as
  8495.          _v_o_l_a_t_i_l_e (see vvoollaattiillee//11) are  nnoott saved.  Neither  are foreign
  8496.          predicates (see also below).
  8497.  
  8498.      4.  The _r_e_c_o_r_d  _s_e_c_t_i_o_n contains  the database  records saved  with
  8499.          rreeccoorrddaa//33  and friends.    The  current version  saves  records
  8500.          using directives.
  8501.  
  8502.      5.  The _f_l_a_g  _s_e_c_t_i_o_n contains  the  global flags  saved using  the
  8503.          ffllaagg//33 predicate.  Flags are saved as directives.
  8504.  
  8505.      6.  The  _f_e_a_t_u_r_e  _s_e_c_t_i_o_n   contains  all  features  that  do   not
  8506.          originate from the emulator itself.  See sseett__ffeeaattuurree//22.
  8507.  
  8508.      7.  The _i_m_p_o_r_t  _s_e_c_t_i_o_n contains  the imports  as far  as they  are
  8509.          not handled  by the  auto-import system.   That  is, an  import
  8510.          is stored  if the module  is user or  the module user  contains
  8511.          a  different definition  as  the  one imported  in  the  target
  8512.          module.'
  8513.  
  8514.     Before   writing  the  data   to  file,   qqssaavvee__pprrooggrraamm//22 will   run
  8515.     aauuttoollooaadd//00  to all  required  autoloading the  system can  discover.
  8516.     See aauuttoollooaadd//00.
  8517.  
  8518.     Provided  the  application  does  not  require  any  of  the  Prolog
  8519.     libraries  to  be  loaded  at  runtime,   the  only  file  from  the
  8520.     SWI-Prolog development  environment required is the emulator itself.
  8521.     The  emulator may  be built in  two flavours.   The  default is  the
  8522.     _d_e_v_e_l_o_p_m_e_n_t  _e_m_u_l_a_t_o_r.  The  _r_u_n_t_i_m_e _e_m_u_l_a_t_o_r is similar, but  lacks
  8523.     the  tracer.  The stand-alone program cchhppll((1)) may be used  to change
  8524.     the default path to the emulator.
  8525.  
  8526.     If the  option stand_alone(on)is present,  the emulator is prepended
  8527.     for  the  state.    If  the emulator  is  started  and no  state  is
  8528.     specified  using  the -x  flag,  it will  test whether  a  boot-file
  8529.     (state)  is attached  to the  emulator itself and  load this  state.
  8530.     Provided  the application  has all libraries  loaded, the  resulting
  8531.     file may be started anywhere.
  8532.  
  8533.  
  8534. qqssaavvee__pprrooggrraamm((_+_F_i_l_e))
  8535.     Equivalent to qsave_program(File, []).
  8536.  
  8537.  
  8538. aauuttoollooaadd
  8539.     Check  the current Prolog program  for predicates that are  referred
  8540.     to,  are  undefined and  have a  definition in  the Prolog  library.
  8541.     Load the appropriate libraries.
  8542.  
  8543.     This  predicate is used  by qqssaavvee__pprrooggrraamm//[[11,,22]] to ensure the  saved
  8544.     state  will not  depend  on one  of the  libraries.   The  predicate
  8545.     aauuttoollooaadd//00  will find all ddiirreecctt references to predicates.   It does
  8546.     not  find predicates referenced via meta-predicates.   The predicate
  8547.     log/2  is  defined  in the  library(quintus)  to provide  a  quintus
  8548.     compatible means to compute  the natural logarithm of a number.  The
  8549.     following program will  behave correctly if its state is executed in
  8550.     an environment where the library(quintus) is not available:
  8551.  
  8552.     logtable(From, To) :-
  8553.             From > To, !.
  8554.     logtable(From, To) :-
  8555.             log(From, Value),
  8556.             format('~d~t~8|~2f~n', [From, Value]),
  8557.             F is From + 1,
  8558.             logtable(F, To).
  8559.  
  8560.     However,  the following implementation  refers to log/2 through  the
  8561.     meta-predicate  mmaapplliisstt//33.   Autoload will not  be able to find  the
  8562.     reference.   This problem may be fixed either by loading  the module
  8563.     libtary(quintus)  explicitly or  use  rreeqquuiirree//11 to  tell the  system
  8564.     that the predicate log/2 is required by this module.
  8565.  
  8566.     logtable(From, To) :-
  8567.             findall(X, between(From, To, X), Xlist),
  8568.             maplist(log, Xlist, SineList),
  8569.             write_table(Xlist, SineList).
  8570.  
  8571.     write_table([], []).
  8572.     write_table([I|IT], [V|VT]) :-
  8573.             format('~d~t~8|~2f~n', [I, V]),
  8574.             write_table(IT, VT).
  8575.  
  8576.  
  8577. vvoollaattiillee _+_N_a_m_e_/_A_r_i_t_y_, _._._.
  8578.     Declare  that  the clauses  of specified  predicates  should nnoott  be
  8579.     saved  to the program.   The  volatile declaration is normally  used
  8580.     to avoid that  the clauses of dynamic predicates that represent data
  8581.     for the current session is saved in the state file.
  8582.  
  8583.  
  8584. 66..11 LLiimmiittaattiioonnss ooff qqssaavvee__pprrooggrraamm
  8585.  
  8586. There  are  three  areas  that  require  special  attention  when  using
  8587. qqssaavvee__pprrooggrraamm//[[11,,22]].
  8588.  
  8589.   o If  the  program  is an  embedded  Prolog  application or  uses  the
  8590.     foreign  language interface,  care has  to be taken  to restore  the
  8591.     appropriate foreign context.  See section 6.2 for details.
  8592.  
  8593.   o If  the program uses directives (:- goal. lines) that  perform other
  8594.     actions then  setting predicate attributes (dynamic, volatile, etc.)
  8595.     or  loading files  (consult,  etc.), the  directive may  need to  be
  8596.     prefixed with iinniittiiaalliizzaattiioonn//11.
  8597.  
  8598.   o `Database references as  returned by ccllaauussee//33, rreeccoorrddeedd//33, etc.  are
  8599.     not preserved and may thus not be part of the database when saved.
  8600.  
  8601.  
  8602. 66..22 RRuunnttiimmeess aanndd FFoorreeiiggnn CCooddee
  8603.  
  8604. Some  applications may  need  to  use the  foreign  language  interface.
  8605. Object code is  by definition machine-dependent and thus cannot  be part
  8606. of the saved program file.
  8607.  
  8608. To complicate the matter even further there are various  ways of loading
  8609. foreign code:
  8610.  
  8611.   o _U_s_i_n_g _t_h_e _l_i_b_r_a_r_y_(_s_h_l_i_b_) _p_r_e_d_i_c_a_t_e_s
  8612.     This  is the preferred way of dealing  with foreign code.   It loads
  8613.     quickly and ensures  an acceptable level of independence between the
  8614.     versions  of the emulator  and the  foreign code loaded.   It  works
  8615.     on  Unix machines supporting shared libraries and  library functions
  8616.     to  load them.   Most modern  Unixes satisfy this  constraint..   It
  8617.     also  works on the Win32 platform:  Windows-NT, '95 and  Windows 3.1
  8618.     running win32s.
  8619.  
  8620.   o _S_t_a_t_i_c _l_i_n_k_i_n_g
  8621.     This  mechanism works on  all machines,  but generally requires  the
  8622.     same  C-compiler and linker to be  used for the external code as  is
  8623.     used  to build SWI-Prolog itself.   This mechanism is the  preferred
  8624.     way if shared libraries are not supported.
  8625.  
  8626.   o _U_s_i_n_g llooaadd__ffoorreeiiggnn//[[22,,55]]
  8627.     Basically  only works  on Unix  system supporting  the a.out  format
  8628.     executables.   This mechanism is  slow and non-portable.  It  should
  8629.     be avoided whenever possible.
  8630.  
  8631. To make  a runtime  executable that  can run on  multiple platforms  one
  8632. must make runtime  checks to find the correct  way of linking.   Suppose
  8633. we have  a source-file  myextension defining  the installation  function
  8634. iinnssttaallll(()).
  8635.  
  8636. If  this file  is compiled  to a  shared library,  llooaadd__ffoorreeiiggnn__lliibbrraarryy//11
  8637. will load this library and call the installation  function to initialise
  8638. the  foreign code.    If it  is  loaded as  a static  extension,  define
  8639. iinnssttaallll(()) as the predicate iinnssttaallll//00:
  8640.  
  8641. static foreign_t
  8642. pl_install()
  8643. { install();
  8644.  
  8645.   PL_succeed;
  8646. }
  8647.  
  8648. PL_extension PL_extensions [] =
  8649. {
  8650. /*{ "name",     arity,  function,       PL_FA_<flags> },*/
  8651.  
  8652.   { "install",  0,      pl_install,     0 },
  8653.   { NULL,       0,      NULL,           0 }     /* terminating line */
  8654. };
  8655.  
  8656. Now, use the following Prolog code to load the foreign library:
  8657.  
  8658. load_foreign_extensions :-
  8659.         current_predicate(install, install), !, % static loaded
  8660.         install.
  8661. load_foreign_extensions :-                      % shared library
  8662.         load_foreign_library(foreign(myextension)).
  8663.  
  8664. :- initialization load_foreign_extensions.
  8665.  
  8666. The path  alias foreign  is defined  by ffiillee__sseeaarrcchh__ppaatthh//22.   By  default
  8667. it  searches  the  directories <_h_o_m_e>/lib/<_a_r_c_h> and  <_h_o_m_e>/lib.     The
  8668. application can specify additional rules for ffiillee__sseeaarrcchh__ppaatthh//22.
  8669.  
  8670.  
  8671. 66..33 FFiinnddiinngg AApppplliiccaattiioonn ffiilleess
  8672.  
  8673. If your application  uses files that are  not part of the saved  program
  8674. such as database  files, configuration files, etc., the  runtime version
  8675. has to be able  to locate these files.   The ffiillee__sseeaarrcchh__ppaatthh//22mechanism
  8676. in combination with  the -palias command-line argument is the  preferred
  8677. way to  locate runtime  files.   The first  step is to  define an  alias
  8678. for the  toplevel directory  of your  application.   We  will call  this
  8679. directory gnatdir in our examples.
  8680.  
  8681. A  good  place  for storing  data  associated  with  SWI-Prolog  runtime
  8682. systems is  below the emulator's  home-directory.   swi is a  predefined
  8683. alias for this directory.  The following is  a useful default definition
  8684. for the search path.
  8685.  
  8686. user:file_search_path(gnatdir, swi(gnat)).
  8687.  
  8688. The  application  should  locate   all  files  using  absolute_file_name.
  8689. Suppose   gnatdir   contains   a  file   config.pl   to   define   local
  8690. configuration.  Then use the code below to load this file:
  8691.  
  8692. configure_gnat :-
  8693.         (   absolute_file_name(gnatdir('config.pl'), ConfigFile)
  8694.         ->  consult(ConfigFile)
  8695.         ;   format(user_error, 'gnat: Cannot locate config.pl~n'),
  8696.             halt(1)
  8697.         ).
  8698.  
  8699.  
  8700. 66..44 UUssiinngg cchhppll ffoorr CCoonnffiigguurraattiioonn IInnffoorrmmaattiioonn
  8701.  
  8702.  
  8703. 66..44..11 CChhaannggiinngg tthhee eemmuullaattoorr ooff aa rruunnttiimmee aapppplliiccaattiioonn
  8704.  
  8705. The program chpl, may  be used to manipulate the header of  a SWI-Prolog
  8706. bootfile or state created with qqssaavvee__pprrooggrraamm//[[11,,22]].
  8707.  
  8708. It will be used  most commonly by the installer of a  SWI-Prolog runtime
  8709. application  to specify  the path  to the  emulator.    If the  end-user
  8710. decided to install the SWI-Prolog runtime environment in
  8711.  
  8712. /usr/local/lib/rt-pl-2.1.4
  8713.  
  8714. the gnat application can be told to use this emulator using:
  8715.  
  8716. % /usr/local/lib/rtpl-2.1.4/bin/chpl -e /usr/local/lib/rt-pl-
  8717. 2.1.4/bin/pl gnat
  8718.  
  8719. Now,  gnat may  be  installed in  any public  or private  directory  for
  8720. binaries.
  8721.  
  8722.  
  8723. 66..44..22 PPaassssiinngg aa ppaatthh ttoo tthhee aapppplliiccaattiioonn
  8724.  
  8725. Suppose   the  system   administrator  has   installed  the   SWI-Prolog
  8726. runtime  environment  in  /usr/local/lib/rt-pl-2.1.4.     A  user  wants
  8727. to  install  gnat,   but  gnat  will  look  for  its   configuration  in
  8728. /usr/local/lib/rt-pl-2.1.4/gnat where the user cannot write.
  8729.  
  8730. The   user   decides   to   install   the   gnat    runtime   files   in
  8731. /users/bob/lib/gnat.     For   one-time  usage,  the  user   may  decide
  8732. to start gnat using the command:
  8733.  
  8734. % gnat -p gnatdir=/users/bob/lib/gnat
  8735.  
  8736. For a more  widely used executable, this is  not very comfortable.   The
  8737. user may decide  to edit the shell-script part  of gnat.  Upto the  line
  8738. holding
  8739.  
  8740. # End Header
  8741.  
  8742. gnat is a  simple /bin/sh script.   After this line, the file is  binary
  8743. and may  contain long lines.   Most editors  are not capable of  editing
  8744. such files.   Instead of editing the file directly, the  program cchhppll((1))
  8745. may be used  to extract and replace the  header of gnat.  The  following
  8746. editing sequence  will work  with any  editor capable  of editing  ASCII
  8747. files.
  8748.  
  8749. % chpl -x gnat > gnat.hdr
  8750. % emacs gnat.hdr
  8751. % chpl -h gnat.hdr gnat
  8752.  
  8753. The header may be changed to the following to install gnat properly:
  8754.  
  8755. #!/bin/sh
  8756. # SWI-Prolog version: 2.1.4
  8757. # SWI-Prolog save-version: 25
  8758. exec ${SWIPL-/usr/local/lib/rt-pl-2.1.4/bin/pl} -x $0 \
  8759.      -p gnatdir=/users/bob/lib/gnat "$@"
  8760.  
  8761.  
  8762. 66..55 TThhee RRuunnttiimmee EEnnvviirroonnmmeenntt
  8763.  
  8764.  
  8765. 66..55..11 TThhee RRuunnttiimmee EEmmuullaattoorr
  8766.  
  8767. The sources  may be  used to built  two versions  of the emulator.    By
  8768. default,  the _d_e_v_e_l_o_p_m_e_n_t _e_m_u_l_a_t_o_r  is built.    This emulator  contains
  8769. all features  for interactive development  of Prolog  applications.   If
  8770. the system is  configured using --enable-runtime, mmaakkee((1)) will  create a
  8771. _r_u_n_t_i_m_e _v_e_r_s_i_o_n  of the emulator.   This emulator  is equivalent to  the
  8772. development version, except for the following features:
  8773.  
  8774.   o _N_o _i_n_p_u_t _e_d_i_t_i_n_g
  8775.     The  GNU library -lreadline  that provides EMACS compatible  editing
  8776.     of input lines will not be linked to the system.
  8777.  
  8778.   o _N_o _t_r_a_c_e_r
  8779.     The  tracer and  all its options  are removed,  making the system  a
  8780.     little faster too.
  8781.  
  8782.   o _N_o _p_r_o_f_i_l_e_r
  8783.     pprrooffiillee//33 and friends are  not supported.  This saves some space and
  8784.     provides better performance.
  8785.  
  8786.   o _N_o _i_n_t_e_r_r_u_p_t
  8787.     Keyboard  interrupt (Control-C  normally)  is not  rebound and  will
  8788.     normally terminate the application.
  8789.  
  8790.   o _f_e_a_t_u_r_e_(_r_u_n_t_i_m_e_, _t_r_u_e_) _s_u_c_c_e_e_d_s
  8791.     This  may  be used  to verify  your application  is  running in  the
  8792.     runtime environment rather than the development environment.
  8793.  
  8794.   o ccllaauussee//[[22,,33]] _d_o _n_o_t _w_o_r_k _o_n _s_t_a_t_i_c _p_r_e_d_i_c_a_t_e_s
  8795.     This  feature inhibits  listing your  program.   It is  only a  very
  8796.     limited protection however.
  8797.  
  8798. The  following  fragment   is  an  example  for  building  the   runtime
  8799. environment in  HOME/lib/rt-pl-2.1.4.   If possible, the  shared-library
  8800. interface should be configured to ensure it can serve  a large number of
  8801. applications.
  8802.  
  8803. % cd pl-2.1.4
  8804. % mkdir runtime
  8805. % cd runtime
  8806. % ../src/configure --enable-runtime --prefix=$HOME
  8807. % make
  8808. % make rt-install
  8809.  
  8810. The  runtime directory  contains  the components  listed  below.    This
  8811. directory may be tar'ed and shipped with your application.
  8812.  
  8813.             ________________________________________________
  8814.             |_README.RT__|Info_on_the_runtime_environment___|
  8815.             | bin/pl     |The emulator itself               |
  8816.  
  8817.             |_bin/chpl___|The_utility_to_change_the_runtime_|
  8818.             | man/chpl.1 |Manual page for chpl              |
  8819.             |_man/pl.1___|Manual_page_for_pl________________|
  8820.             |_swipl______|pointer_to_the_home_directory_(.)_|
  8821.             | lib/       |directory for shared libraries    |
  8822.             |_lib/<_a_r_c_h>/|machine-specific_shared_libraries_|
  8823.  
  8824.  
  8825. CChhaapptteerr 77..  HHAACCKKEERRSS CCOORRNNEERR
  8826.  
  8827. This  appendix  describes  a  number  of  predicates  which  enable  the
  8828. Prolog  user  to  inspect the  Prolog  environment  and  manipulate  (or
  8829. even redefine)  the debugger.    They can be  used as  entry points  for
  8830. experiments with debugging  tools for Prolog.  The  predicates described
  8831. here should  be handled  with some  care as it  is easy  to corrupt  the
  8832. consistency of the Prolog system by misusing them.
  8833.  
  8834.  
  8835. 77..11 EExxaammiinniinngg tthhee EEnnvviirroonnmmeenntt SSttaacckk
  8836.  
  8837.  
  8838. pprroolloogg__ccuurrrreenntt__ffrraammee((_-_F_r_a_m_e))
  8839.     Unify   _F_r_a_m_e  with  an  integer   providing  a  reference  to   the
  8840.     parent  of  the  current  local stack  frame.    A  pointer  to  the
  8841.     current  local frame  cannot be provided  as the predicate  succeeds
  8842.     deterministically  and therefore its frame is  destroyed immediately
  8843.     after succeeding.
  8844.  
  8845.  
  8846. pprroolloogg__ffrraammee__aattttrriibbuuttee((_+_F_r_a_m_e_, _+_K_e_y_, _-_V_a_l_u_e))
  8847.     Obtain  information  about  the local  stack  frame  _F_r_a_m_e.    _F_r_a_m_e
  8848.     is  a  frame reference  as  obtained through  pprroolloogg__ccuurrrreenntt__ffrraammee//11,
  8849.     pprroolloogg__ttrraaccee__iinntteerrcceeppttiioonn//44or  this predicate.   The key values  are
  8850.     described below.
  8851.  
  8852.     aalltteerrnnaattiivvee
  8853.          _V_a_l_u_e is unified with  an integer reference to the  local stack
  8854.          frame in  which execution  is  resumed if  the goal  associated
  8855.          with _F_r_a_m_e  fails.    Fails  if the  frame has  no  alternative
  8856.          frame.
  8857.  
  8858.     hhaass__aalltteerrnnaattiivveess
  8859.          _V_a_l_u_e is unified  with true if _F_r_a_m_e  still is a candidate  for
  8860.          backtracking.  false otherwise.
  8861.  
  8862.     ggooaall
  8863.          _V_a_l_u_e is unified with the  goal associated with _F_r_a_m_e.   If the
  8864.          definition module of the active predicate is not  user the goal
  8865.          is represented as <_m_o_d_u_l_e>:<_g_o_a_l>.  Do not instantiate variables
  8866.          in this goal unless you kknnooww what you are doing!
  8867.  
  8868.     ccllaauussee
  8869.          _V_a_l_u_e is  unified with  a  reference to  the currently  running
  8870.          clause.    Fails  if the  current  goal  is associated  with  a
  8871.          foreign  (C) defined  predicate.    See  also  nntthh__ccllaauussee//33 and
  8872.          ccllaauussee__pprrooppeerrttyy//22.
  8873.  
  8874.     lleevveell
  8875.          _V_a_l_u_e is unified  with the recursion level  of _F_r_a_m_e.  The  top
  8876.          level frame is at level `0'.
  8877.  
  8878.     ppaarreenntt
  8879.          _V_a_l_u_e is unified with an integer reference to  the parent local
  8880.          stack frame of _F_r_a_m_e.  Fails if _F_r_a_m_e is the top frame.
  8881.  
  8882.     ccoonntteexxtt__mmoodduullee
  8883.          _V_a_l_u_e is  unified with the  name of the  context module of  the
  8884.          environment.
  8885.  
  8886.     ttoopp
  8887.          _V_a_l_u_e is  unified with  true if _F_r_a_m_e  is the  top Prolog  goal
  8888.          from a recursive  call back from the  foreign language.   false
  8889.          otherwise.
  8890.  
  8891.     hhiiddddeenn
  8892.          _V_a_l_u_e is  unified with  true if the  frame is  hidden from  the
  8893.          user, either  because a  parent has  the hide-childs  attribute
  8894.          (all  system  predicates),  or  the  system   has  no  trace-me
  8895.          attribute.
  8896.  
  8897.     ppcc
  8898.          _V_a_l_u_e is unified with  the program-pointer saved on behalve  of
  8899.          the parent-goal if  the parent-goal is  not owned by a  foreign
  8900.          predicate.
  8901.  
  8902.     aarrgguummeenntt((_N))
  8903.          _V_a_l_u_e is  unified with the _N-th  slot of the  frame.   Argument
  8904.          1 is  the first  argument of  the goal.    Arguments above  the
  8905.          arity refer to local variables.  Fails silently if _N  is out of
  8906.          range.
  8907.  
  8908.  
  8909. 77..22 IInntteerrcceeppttiinngg tthhee TTrraacceerr
  8910.  
  8911.  
  8912. pprroolloogg__ttrraaccee__iinntteerrcceeppttiioonn((_+_P_o_r_t_, _+_F_r_a_m_e_, _+_P_C_, _-_A_c_t_i_o_n))
  8913.     Dynamic  predicate, normally not defined.  This predicate  is called
  8914.     from  the SWI-Prolog debugger just before it would show a port.   If
  8915.     this  predicate succeeds the debugger  assumes the trace action  has
  8916.     been  taken care of and continues execution as described  by _A_c_t_i_o_n.
  8917.     Otherwise the normal Prolog debugger actions are performed.
  8918.  
  8919.     _P_o_r_t  is one  of call,  redo,  exit, fail  or unify.    _F_r_a_m_e is  an
  8920.     integer  reference to  the current  local stack frame.    _P_C is  the
  8921.     current  value  of the  program-counter, relative  to  the start  of
  8922.     the  current clause,  or 0  if it  is invalid,  for example  because
  8923.     the  current frame runs a foreign  predicate, or no clause has  been
  8924.     selected  yet.    _A_c_t_i_o_n should  be unified  with one  of the  atoms
  8925.     continue  (just continue execution), retry (retry the  current goal)
  8926.     or fail (force the  current goal to fail).  Leaving it a variable is
  8927.     identical to continue.
  8928.  
  8929.     Together  with  the predicates  described in  section  3.37 and  the
  8930.     other  predicates of this chapter this predicate enables  the Prolog
  8931.     user  to define a complete new debugger in Prolog.  Besides  this it
  8932.     enables  the Prolog programmer monitor  the execution of a  program.
  8933.     The  example below records  all goals trapped  by the tracer in  the
  8934.     database.
  8935.  
  8936.     prolog_trace_interception(Port, Frame, _PC, continue) :-
  8937.             prolog_frame_attribute(Frame, goal, Goal),
  8938.             prolog_frame_attribute(Frame, level, Level),
  8939.             recordz(trace, trace(Port, Level, Goal)).
  8940.  
  8941.     To  trace the execution of `go' this way the following  query should
  8942.     be given:
  8943.  
  8944.     ?- trace, go, notrace.
  8945.  
  8946.  
  8947. pprroolloogg__sskkiipp__lleevveell((_-_O_l_d_, _+_N_e_w))
  8948.     Unify  _O_l_d with  the old  value of `skip  level' and  than set  this
  8949.     level  according to _N_e_w.   New  is an integer,  or the special  atom
  8950.     very_deep  (meaning don't  skip).    The `skip  level' is  a  global
  8951.     variable  of the  Prolog system  that disables the  debugger on  all
  8952.     recursion  levels deeper than  the level of the  variable.  Used  to
  8953.     implement  the trace options  `skip' (sets skip  level to the  level
  8954.     of  the frame) and `up' (sets skip level to the level of  the parent
  8955.     frame (i.e. the level of this frame minus 1).
  8956.  
  8957.  
  8958. 77..33 EExxcceeppttiioonn HHaannddlliinngg
  8959.  
  8960. A  start has  been made  to  make exception  handling available  to  the
  8961. Prolog user.   On exceptions a  dynamic and multifile defined  predicate
  8962. eexxcceeppttiioonn//33 is called.   If this user defined predicate  succeeds Prolog
  8963. assumes the  exception has  been taken care  of.   Otherwise the  system
  8964. default exception handler is called.
  8965.  
  8966.  
  8967. eexxcceeppttiioonn((_+_E_x_c_e_p_t_i_o_n_, _+_C_o_n_t_e_x_t_, _-_A_c_t_i_o_n))
  8968.     Dynamic  predicate,  normally not  defined.   Called  by the  Prolog
  8969.     system  on run-time exceptions.  Currently eexxcceeppttiioonn//33 is  only used
  8970.     for  trapping undefined  predicates.   Future versions might  handle
  8971.     signal  handling, floating exceptions  and other runtime errors  via
  8972.     this mechanism.  The values for _E_x_c_e_p_t_i_o_n are described below.
  8973.  
  8974.     uunnddeeffiinneedd__pprreeddiiccaattee
  8975.          If _E_x_c_e_p_t_i_o_n is undefined_predicate _C_o_n_t_e_x_t is  instantiated to
  8976.          a term _N_a_m_e/_A_r_i_t_y.   _N_a_m_e refers to  the name and _A_r_i_t_y to  the
  8977.          arity of  the undefined predicate.    If the definition  module
  8978.          of the  predicate is  not _u_s_e_r,  _C_o_n_t_e_x_t  will be  of the  form
  8979.          <_M_o_d_u_l_e>:<_N_a_m_e>/<_A_r_i_t_y>.    If the  predicate  fails Prolog  will
  8980.          print the  default error  warning  and start  the tracer.    If
  8981.          the predicate succeeds it should instantiate the  last argument
  8982.          either to the  atom fail to tell  Prolog to fail the  predicate
  8983.          or the atom retry to tell Prolog to retry the predicate.   This
  8984.          only  makes sense  if the  exception  handler has  defined  the
  8985.          predicate.  Otherwise it will lead to a loop.
  8986.  
  8987.     wwaarrnniinngg
  8988.          If prolog  wants to  give a warning  while reading  a file,  it
  8989.          will first raise the  exception _w_a_r_n_i_n_g.  The  context argument
  8990.          is  a  term  of  the  form  warning(<_P_a_t_h>, <_L_i_n_e_N_o>, <_M_e_s_s_a_g_e>),
  8991.          where _P_a_t_h  is  the absolute  filename of  the  file prolog  is
  8992.          reading; _L_i_n_e_N_o  is an  estimate of the  line number where  the
  8993.          error occurred and  _M_e_s_s_a_g_e is a  Prolog string indicating  the
  8994.          message.    The _A_c_t_i_o_n  argument  is ignored.    The  error  is
  8995.          supposed to be presented  to the user if the  exception handler
  8996.          succeeds.   Otherwise  the standard Prolog  warning message  is
  8997.          printed.
  8998.  
  8999.          This exception  is used  by the library(emacs_interface),  that
  9000.          integrates error handling with GNU Emacs.
  9001.  
  9002.  
  9003. 77..44 RReeaaddlliinnee IInntteerraaccttiioonn
  9004.  
  9005. The  following  predicates  are  available   if  feature(readline, true)
  9006. succeeds.    They allow  for direct  interaction with  the GNU  readline
  9007. library.  See also rreeaaddlliinnee((3))
  9008.  
  9009.  
  9010. rrll__rreeaadd__iinniitt__ffiillee((_+_F_i_l_e))
  9011.     Read  a readline  initialisation file.   Readline  by default  reads
  9012.     ~/.inputrc.     This  predicate  may be  used  to  read  alternative
  9013.     readline initialisation files.
  9014.  
  9015.  
  9016. rrll__aadddd__hhiissttoorryy((_+_L_i_n_e))
  9017.     Add  a  line  to  the  Control-P/Control-N  history  system  of  the
  9018.     readline library.
  9019.  
  9020.  
  9021. CChhaapptteerr 88..  SSUUMMMMAARRYY
  9022.  
  9023.  
  9024. 88..11 PPrreeddiiccaatteess
  9025.  
  9026. The  predicate summary  is used  by the  Prolog  predicate aapprrooppooss//11  to
  9027. suggest predicates from a keyword.
  9028.  
  9029.  !/0                           Cut (discard choicepoints)
  9030.  !/1                           Cut block.  See bblloocckk//33
  9031.  ,/2                           Conjunction of goals
  9032.  ->/2                          If-then-else
  9033.  *->/2                         Soft-cut
  9034.  ./2                           Consult.  Also list constructor
  9035.  ;/2                           Disjunction of goals.  Same as |//22
  9036.  </2                           Arithmetic smaller
  9037.  
  9038.  =/2                           Unification
  9039.  =../2                         ``Univ.''  Term to list conversion
  9040.  =:=/2                         Arithmetic equal
  9041.  =</2                          Arithmetic smaller or equal
  9042.  ==/2                          Identical
  9043.  =@=/2                         Structural identical
  9044.  =\=/2                         Arithmetic not equal
  9045.  
  9046.  >/2                           Arithmetic larger
  9047.  >=/2                          Arithmetic larger or equal
  9048.  @</2                          Standard order smaller
  9049.  @=</2                         Standard order smaller or equal
  9050.  @>/2                          Standard order larger
  9051.  @>=/2                         Standard order larger or equal
  9052.  \+/1                          Negation by failure.  Same as nnoott//11
  9053.  \=/2                          Not unifyable
  9054.  
  9055.  \==/2                         Not identical
  9056.  \=@=/2                        Not structural identical
  9057.  ^/2                           Existential quantification (bbaaggooff//33, sseettooff//33)
  9058.  |/2                           Disjunction of goals.  Same as ;//22
  9059.  abolish/1                     Remove predicate definition from the database
  9060.  abolish/2                     Remove predicate definition from the database
  9061.  abort/0                       Abort execution, return to top level
  9062.  
  9063.  absolute_file_name/2          Get absolute path name
  9064.  absolute_file_name/3          Get absolute path name with options
  9065.  access_file/2                 Check access permissions of a file
  9066.  append/1                      Append to a file
  9067.  append/3                      Concatenate lists
  9068.  apply/2                       Call goal with additional arguments
  9069.  apropos/1                     library(online_help) Show related predicates and manual sections
  9070.  arg/3                         Access argument of a term
  9071.  
  9072.  arithmetic_function/1         Register an evaluable function
  9073.  assert/1                      Add a clause to the database
  9074.  assert/2                      Add a clause to the database, give reference
  9075.  asserta/1                     Add a clause to the database (first)
  9076.  asserta/2                     Add a clause to the database (first)
  9077.  assertz/1                     Add a clause to the database (last)
  9078.  assertz/2                     Add a clause to the database (last)
  9079.  
  9080.  at_end_of_stream/0            Test for end of file on input
  9081.  at_end_of_stream/1            Test for end of file on stream
  9082.  at_halt/1                     Register goal to run at hhaalltt//11
  9083.  at_initialization/1           Register goal to run at start-up
  9084.  atom/1                        Type check for an atom
  9085.  atom_char/2                   Convert between atom and ASCII value
  9086.  atom_chars/2                  Convert between atom and list of ASCII values
  9087.  atom_length/2                 Determine length of an atom
  9088.  
  9089.  atom_prefix/2                 Test for start of atom
  9090.  atom_to_term/3                Convert between atom and term
  9091.  atomic/1                      Type check for primitive
  9092.  autoload/0                    Autoload all predicates now
  9093.  bagof/3                       Find all solutions to a goal
  9094.  between/3                     Integer range checking/generating
  9095.  block/3                       Start a block (`catch'/`throw')
  9096.  
  9097.  break/0                       Start interactive toplevel
  9098.  call/1                        Call a goal
  9099.  call/[2..]                    Call with additional arguments
  9100.  call_dll_function/2           Win32:  Call function in dynamic link library (.dll file)
  9101.  call_shared_object_function/2 UNIX: Call C-function in shared (.so) file
  9102.  call_with_depth_limit/3       Prove goal with bounded depth
  9103.  catch/3                       Call goal, watching for exceptions
  9104.  character_count/2             Get character index on a stream
  9105.  
  9106.  chdir/1                       Change working directory
  9107.  checklist/2                   Invoke goal on all members of a list
  9108.  clause/2                      Get clauses of a predicate
  9109.  clause/3                      Get clauses of a predicate
  9110.  clause_property/2             Get properties of a clause
  9111.  close/1                       Close stream
  9112.  close_dde_conversation/1      Win32:  Close DDE channel
  9113.  
  9114.  close_dll/1                   Win32:  Close dynamic link library (.dll file)
  9115.  close_shared_object/1         UNIX: Close shared library (.so file)
  9116.  compare/3                     Compare, using a predicate to determine the order
  9117.  compiling/0                   Is this a compilation run?
  9118.  compound/1                    Test for compound term
  9119.  concat/3                      Append two atoms
  9120.  concat_atom/2                 Append a list of atoms
  9121.  concat_atom/3                 Append a list of atoms with separator
  9122.  
  9123.  consult/1                     Read (compile) a Prolog source file
  9124.  context_module/1              Get context module of current goal
  9125.  convert_time/8                Convert time stamp
  9126.  copy_term/2                   Make a copy of a term
  9127.  current_arithmetic_function/1 Examine evaluable functions
  9128.  current_atom/1                Examine existing atoms
  9129.  current_flag/1                Examine existing flags
  9130.  
  9131.  current_foreign_library/2     library(shlib) Examine loaded shared libraries (.so files)
  9132.  current_functor/2             Examine existing name/arity pairs
  9133.  current_input/1               Get current input stream
  9134.  current_key/1                 Examine existing database keys
  9135.  current_module/1              Examine existing modules
  9136.  current_module/2              Examine existing modules
  9137.  current_op/3                  Examine current operator declarations
  9138.  current_output/1              Get the current output stream
  9139.  
  9140.  current_predicate/2           Examine existing predicates
  9141.  current_stream/3              Examine open streams
  9142.  dde_current_connection/2      Win32:  Examine open DDE connections
  9143.  dde_current_service/2         Win32:  Examine DDE services provided
  9144.  dde_execute/2                 Win32:  Execute command on DDE server
  9145.  dde_register_service/2        Win32:  Become a DDE server
  9146.  dde_request/3                 Win32:  Make a DDE request
  9147.  
  9148.  dde_poke/3                    Win32:  POKE operation on DDE server
  9149.  dde_unregister_service/1      Win32:  Terminate a DDE service
  9150.  debug/0                       Test for debugging mode
  9151.  debugging/0                   Show debugger status
  9152.  default_module/2              Get the default modules of a module
  9153.  delete/3                      Delete all matching members from a list
  9154.  delete_file/1                 Remove a file from the file system
  9155.  discontiguous/1               Indicate distributed definition of a predicate
  9156.  
  9157.  dup_stream/2                  Duplicate I/O streams
  9158.  dwim_match/2                  Atoms match in ``Do What I Mean'' sense
  9159.  dwim_match/3                  Atoms match in ``Do What I Mean'' sense
  9160.  dwim_predicate/2              Find predicate in ``Do What I Mean'' sense
  9161.  dynamic/1                     Indicate predicate definition may change
  9162.  ed/0                          Edit last edited predicate
  9163.  ed/1                          Edit a predicate
  9164.  
  9165.  edit/0                        Edit last edited file
  9166.  edit/1                        Edit a file
  9167.  edit_source/1                 (hook) Intercept editing
  9168.  ensure_loaded/1               Consult a file if that has not yet been done
  9169.  erase/1                       Erase a database record or clause
  9170.  exception/3                   (hook) Handle runtime exceptions
  9171.  exists_directory/1            Check existence of directory
  9172.  exists_file/1                 Check existence of file
  9173.  
  9174.  exit/2                        Exit from named block.  See bblloocckk//33
  9175.  expand_answer/2               Expand answer of query
  9176.  expand_file_name/2            Wildcard expansion of file names
  9177.  expand_file_search_path/2     Wildcard expansion of file paths
  9178.  expand_query/4                Expanded entered query
  9179.  expand_term/2                 Compiler:  expand read term into clause(s)
  9180.  explain/1                     library(explain) Explain argument
  9181.  
  9182.  explain/2                     library(explain) 2nd argument is explanation of first
  9183.  export/1                      Export a predicate from a module
  9184.  export_list/2                 List of public predicates of a module
  9185.  fail/0                        Always false
  9186.  fail/1                        Immediately fail named block.  See bblloocckk//33
  9187.  feature/2                     Get system configuration parameters
  9188.  file_base_name/2              Get file part of path
  9189.  file_directory_name/2         Get directory part of path
  9190.  
  9191.  file_name_extension/3         Add, remove or test file extensions
  9192.  file_search_path/2            Define path-aliases for locating files
  9193.  fileerrors/2                  Do/Don't warn on file errors
  9194.  findall/3                     Find all solutions to a goal
  9195.  flag/3                        Simple global variable system
  9196.  flatten/2                     Transform nested list into flat list
  9197.  float/1                       Type check for a floating point number
  9198.  
  9199.  flush/0                       Output pending characters on current stream
  9200.  flush_output/1                Output pending characters on specified stream
  9201.  forall/2                      Prove goal for all solutions of another goal
  9202.  foreign_file/1                Examine loaded foreign files
  9203.  format/1                      Formatted output
  9204.  format/2                      Formatted output with arguments
  9205.  format/3                      Formatted output on a stream
  9206.  format_predicate/2            Program ffoorrmmaatt//[[11,,22]]
  9207.  
  9208.  free_variables/2              Find unbound variables in a term
  9209.  functor/3                     Get name and arity of a term or construct a term
  9210.  garbage_collect/0             Invoke the garbage collector
  9211.  gensym/2                      Generate unique atoms from a base
  9212.  get/1                         Read first non-blank character
  9213.  get/2                         Read first non-blank character from a stream
  9214.  get0/1                        Read next character
  9215.  
  9216.  get0/2                        Read next character from a stream
  9217.  get_single_char/1             Read next character from the terminal
  9218.  get_time/1                    Get current time
  9219.  getenv/2                      Get shell environment variable
  9220.  ground/1                      Verify term holds no unbound variables
  9221.  halt/0                        Exit from Prolog
  9222.  halt/1                        Exit from Prolog with status
  9223.  hash_term/2                   Hash-value of ground term
  9224.  
  9225.  help/0                        Give help on help
  9226.  help/1                        Give help on predicates and show parts of manual
  9227.  ignore/1                      Call the argument, but always succeed
  9228.  import/1                      Import a predicate from a module
  9229.  index/1                       Change clause indexing
  9230.  initialization/1              Initialization directive
  9231.  int_to_atom/2                 Convert from integer to atom
  9232.  
  9233.  int_to_atom/3                 Convert from integer to atom (non-decimal)
  9234.  integer/1                     Type check for integer
  9235.  intersection/3                Set intersection
  9236.  is/2                          Evaluate arithmetic expression
  9237.  is_absolute_file_name/1       True if arg defines an absolute path
  9238.  is_list/1                     Type check for a list
  9239.  is_set/1                      Type check for a set
  9240.  keysort/2                     Sort, using a key
  9241.  
  9242.  last/2                        Last element of a list
  9243.  leash/1                       Change ports visited by the tracer
  9244.  length/2                      Length of a list
  9245.  library_directory/1           (hook) Directories holding Prolog libraries
  9246.  limit_stack/2                 Limit stack expansion
  9247.  line_count/2                  Line number on stream
  9248.  line_position/2               Character position in line on stream
  9249.  
  9250.  list_to_set/2                 Remove duplicates
  9251.  listing/0                     List program in current module
  9252.  listing/1                     List predicate
  9253.  load_files/2                  Load source files with options
  9254.  load_foreign/2                Load foreign (C) module
  9255.  load_foreign/5                Load foreign (C) module
  9256.  load_foreign_library/1        library(shlib) Load shared library (.so file)
  9257.  load_foreign_library/2        library(shlib) Load shared library (.so file)
  9258.  
  9259.  make/0                        Reconsult all changed source files
  9260.  make_fat_filemap/1            Win32:  Create file containing non-FAT filenames
  9261.  make_library_index/1          Create autoload file INDEX.pl
  9262.  maplist/3                     Transform all elements of a list
  9263.  member/2                      Element is member of a list
  9264.  memberchk/2                   Deterministic mmeemmbbeerr//22
  9265.  merge/3                       Merge two sorted lists
  9266.  
  9267.  merge_set/3                   Merge two sorted sets
  9268.  message_hook/3                Intercept pprriinntt__mmeessssaaggee//22
  9269.  meta_predicate/1              Quintus compatibility
  9270.  module/1                      Query/set current type-in module
  9271.  module/2                      Declare a module
  9272.  module_transparent/1          Indicate module based meta predicate
  9273.  msort/2                       Sort, do not remove duplicates
  9274.  multifile/1                   Indicate distributed definition of predicate
  9275.  
  9276.  name/2                        Convert between atom and list of ASCII characters
  9277.  nl/0                          Generate a newline
  9278.  nl/1                          Generate a newline on a stream
  9279.  nodebug/0                     Disable debugging
  9280.  nonvar/1                      Type check for bound term
  9281.  noprotocol/0                  Disable logging of user interaction
  9282.  nospy/1                       Remove spy point
  9283.  
  9284.  nospyall/0                    Remove all spy points
  9285.  not/1                         Negation by failure (argument not provable).  Same as \+//11
  9286.  notrace/0                     Stop tracing
  9287.  notrace/1                     Do not debug argument goal
  9288.  nth0/3                        N-th element of a list (0-based)
  9289.  nth1/3                        N-th element of a list (1-based)
  9290.  nth_clause/3                  N-th clause of a predicate
  9291.  number/1                      Type check for integer or float
  9292.  
  9293.  number_chars/2                Convert between number and atom
  9294.  numbervars/4                  Enumerate unbound variables of a term using a given base
  9295.  once/1                        Call a goal deterministically
  9296.  op/3                          Declare an operator
  9297.  open/3                        Open a file (creating a stream)
  9298.  open/4                        Open a file (creating a stream)
  9299.  open_dde_conversation/3       Win32:  Open DDE channel
  9300.  
  9301.  open_null_stream/1            Open a stream to discard output
  9302.  open_shared_object/2          UNIX: Open shared library (.so file)
  9303.  open_shared_object/3          UNIX: Open shared library (.so file)
  9304.  peek_byte/1                   Read character without removing
  9305.  peek_byte/2                   Read character without removing
  9306.  phrase/2                      Activate grammar-rule set
  9307.  phrase/3                      Activate grammar-rule set (returning rest)
  9308.  please/3                      Query/change environment parameters
  9309.  
  9310.  plus/3                        Logical integer addition
  9311.  portray/1                     (hook) Modify behaviour of pprriinntt//11
  9312.  portray_clause/1              Pretty print a clause
  9313.  predicate_property/2          Query predicate attributes
  9314.  predsort/3                    Sort, using a predicate to determine the order
  9315.  preprocessor/2                Install a preprocessor before the compiler
  9316.  print/1                       Print a term
  9317.  
  9318.  print/2                       Print a term on a stream
  9319.  print_message/2               Print message from (exception) term
  9320.  profile/3                     Obtain execution statistics
  9321.  profile_count/3               Obtain profile results on a predicate
  9322.  profiler/2                    Obtain/change status of the profiler
  9323.  prolog/0                      Run interactive toplevel
  9324.  prolog_current_frame/1        Reference to goal's environment stack
  9325.  prolog_frame_attribute/3      Obtain information on a goal environment
  9326.  
  9327.  prolog_load_context/2         Context information for directives
  9328.  prolog_skip_level/2           Indicate deepest recursion to trace
  9329.  prolog_to_os_filename/2       Convert between Prolog and OS filenames
  9330.  prolog_trace_interception/4   library(user) Intercept the Prolog tracer
  9331.  prompt1/1                     Change prompt for 1 line
  9332.  prompt/2                      Change the prompt used by rreeaadd//11
  9333.  proper_list/1                 Type check for list
  9334.  
  9335.  protocol/1                    Make a log of the user interaction
  9336.  protocola/1                   Append log of the user interaction to file
  9337.  protocolling/1                On what file is user interaction logged
  9338.  put/1                         Write a character
  9339.  put/2                         Write a character on a stream
  9340.  qcompile/1                    Compile source to Quick Load File
  9341.  qload/1                       Load Quick Load File as ccoonnssuulltt//11
  9342.  qsave_program/1               Create runtime application
  9343.  
  9344.  qsave_program/2               Create runtime application
  9345.  read/1                        Read Prolog term
  9346.  read/2                        Read Prolog term from stream
  9347.  read_clause/1                 Read clause
  9348.  read_clause/2                 Read clause from stream
  9349.  read_history/6                Read using history substitution
  9350.  read_link/3                   Read a symbolic link
  9351.  
  9352.  read_term/2                   Read term with options
  9353.  read_term/3                   Read term with options from stream
  9354.  read_variables/2              Read clause including variable names
  9355.  read_variables/3              Read clause including variable names from stream
  9356.  recorda/2                     Record term in the database (first)
  9357.  recorda/3                     Record term in the database (first)
  9358.  recorded/2                    Obtain term from the database
  9359.  recorded/3                    Obtain term from the database
  9360.  
  9361.  recordz/2                     Record term in the database (last)
  9362.  recordz/3                     Record term in the database (last)
  9363.  redefine_system_predicate/1   Abolish system definition
  9364.  rename_file/2                 Change name of file
  9365.  repeat/0                      Succeed, leaving infinite backtrack points
  9366.  require/1                     This file requires these predicates
  9367.  reset_profiler/0              Clear statistics obtained by the profiler
  9368.  
  9369.  restore/1                     Restore saved-state (ssaavvee//11, ssaavvee__pprrooggrraamm//11)
  9370.  retract/1                     Remove clause from the database
  9371.  retractall/1                  Remove unifying clauses from the database
  9372.  reverse/2                     Inverse the order of the elements in a list
  9373.  same_file/2                   Succeeds if arguments refer to same file
  9374.  save/1                        Save program including current goal
  9375.  save/2                        Save program including current goal
  9376.  save_program/1                Save the current program on a file
  9377.  
  9378.  save_program/2                Save the current program on a file
  9379.  see/1                         Change the current input stream
  9380.  seeing/1                      Query the current input stream
  9381.  seen/0                        Close the current input stream
  9382.  select/3                      Select element of a list
  9383.  set_feature/2                 Define a system feature
  9384.  set_input/1                   Set current input stream from a stream
  9385.  
  9386.  set_output/1                  Set current output stream from a stream
  9387.  set_tty/2                     Set `tty' stream
  9388.  setarg/3                      Destructive assignment on term
  9389.  setenv/2                      Set shell environment variable
  9390.  setof/3                       Find all unique solutions to a goal
  9391.  sformat/2                     Format on a string
  9392.  sformat/3                     Format on a string
  9393.  shell/0                       Execute interactive subshell
  9394.  
  9395.  shell/1                       Execute OS command
  9396.  shell/2                       Execute OS command
  9397.  show_profile/1                Show results of the profiler
  9398.  size_file/2                   Get size of a file in characters
  9399.  skip/1                        Skip to character in current input
  9400.  skip/2                        Skip to character on stream
  9401.  rl_add_history/1              Add line to readline(3) history
  9402.  
  9403.  rl_read_init_file/1           Read readline(3) init file
  9404.  sleep/1                       Suspend execution for specified time
  9405.  sort/2                        Sort elements in a list
  9406.  source_file/1                 Examine currently loaded source files
  9407.  source_file/2                 Obtain source file of predicate
  9408.  source_location/2             Location of last read term
  9409.  spy/1                         Force tracer on specified predicate
  9410.  stack_parameter/4             Some systems:  Query/Set runtime stack parameter
  9411.  
  9412.  statistics/0                  Show execution statistics
  9413.  statistics/2                  Obtain collected statistics
  9414.  stream_position/3             Get/seek to position in file
  9415.  string/1                      Type check for string
  9416.  string_concat/3               ccoonnccaatt//33 for strings (non-deterministic)
  9417.  string_length/2               Determine length of a string
  9418.  string_to_atom/2              Conversion between string and atom
  9419.  
  9420.  string_to_list/2              Conversion between string and list of ASCII
  9421.  style_check/1                 Change level of warnings
  9422.  sublist/3                     Determine elements that meet condition
  9423.  subset/2                      Generate/check subset relation
  9424.  substring/4                   Get part of a string
  9425.  subtract/3                    Delete elements that do not meet condition
  9426.  succ/2                        Logical integer successor relation
  9427.  swritef/2                     Formatted write on a string
  9428.  
  9429.  swritef/3                     Formatted write on a string
  9430.  tab/1                         Output number of spaces
  9431.  tab/2                         Output number of spaces on a stream
  9432.  tell/1                        Change current output stream
  9433.  telling/1                     Query current output stream
  9434.  term_expansion/2              (hook) Convert term before compilation
  9435.  term_to_atom/2                Convert between term and atom
  9436.  
  9437.  throw/1                       Raise an exception (see ccaattcchh//33)
  9438.  time/1                        Determine time needed to execute goal
  9439.  time_file/2                   Get last modification time of file
  9440.  tmp_file/2                    Create a temporary filename
  9441.  told/0                        Close current output
  9442.  trace/0                       Start the tracer
  9443.  trace/1                       Set trace-point on predicate
  9444.  trace/2                       Set/Clear trace-point on ports
  9445.  
  9446.  tracing/0                     Query status of the tracer
  9447.  trim_stacks/0                 Release unused memory resources
  9448.  true/0                        Succeed
  9449.  tty_get_capability/3          Get terminal parameter
  9450.  tty_goto/2                    Goto position on screen
  9451.  tty_put/2                     Write control string to terminal
  9452.  ttyflush/0                    Flush output on terminal
  9453.  
  9454.  union/3                       Union of two sets
  9455.  unknown/2                     Trap undefined predicates
  9456.  unload_foreign_library/1      library(shlib) Detach shared library (.so file)
  9457.  unsetenv/1                    Delete shell environment variable
  9458.  use_module/1                  Import a module
  9459.  use_module/2                  Import predicates from a module
  9460.  var/1                         Type check for unbound variable
  9461.  visible/1                     Ports that are visible in the tracer
  9462.  
  9463.  volatile/1                    Predicates that are not saved
  9464.  wait_for_input/3              Wait for input with optional timeout
  9465.  wildcard_match/2              Csh(1) style wildcard match
  9466.  win_exec/2                    Win32:  spawn Windows task
  9467.  write/1                       Write term
  9468.  write/2                       Write term to stream
  9469.  write_ln/1                    Write term, followed by a newline
  9470.  
  9471.  write_canonical/1             Write a term with quotes, ignore operators
  9472.  write_canonical/2             Write a term with quotes, ignore operators on a stream
  9473.  write_term/2                  Write term with options
  9474.  write_term/3                  Write term with options to stream
  9475.  writef/1                      Formatted write
  9476.  writef/2                      Formatted write on stream
  9477.  writeq/1                      Write term, insert quotes
  9478.  writeq/2                      Write term, insert quotes on stream
  9479.  
  9480.  
  9481. 88..22 AArriitthhmmeettiicc FFuunnccttiioonnss
  9482.  
  9483.  */2                     Multiplication
  9484.  **/2                    Power function
  9485.  
  9486.  +/2                     Addition
  9487.  -/1                     Unary minus
  9488.  -/2                     Subtraction
  9489.  //2                     Division
  9490.  ///2                    Integer division
  9491.  /\/2                    Bitwise and
  9492.  <</2                    Bitwise left shift
  9493.  >>/2                    Bitwise right shift
  9494.  
  9495.  ./2                     List of one character:  character code
  9496.  \/1                     Bitwise negation
  9497.  \//2                    Bitwise or
  9498.  ^/2                     Power function
  9499.  abs/1                   Absolute value
  9500.  acos/1                  Inverse (arc) cosine
  9501.  asin/1                  Inverse (arc) sine
  9502.  
  9503.  atan/1                  Inverse (arc) tangent
  9504.  atan/2                  Rectangular to polar conversion
  9505.  ceil/1                  Smallest integer larger than arg
  9506.  ceiling/1               Smallest integer larger than arg
  9507.  cos/1                   Cosine
  9508.  cputime/0               Get CPU time
  9509.  e/0                     Mathematical constant
  9510.  exp/1                   Exponent (base e)
  9511.  
  9512.  float/1                 Explicitly convert to float
  9513.  float_fractional_part/1 Fractional part of a float
  9514.  float_integer_part/1    Integer part of a float
  9515.  floor/1                 Largest integer below argument
  9516.  integer/1               Round to nearest integer
  9517.  log/1                   Natural logarithm
  9518.  log10/1                 10 base logarithm
  9519.  
  9520.  max/2                   Maximum of two numbers
  9521.  min/2                   Minimum of two numbers
  9522.  mod/2                   Remainder of division
  9523.  random/1                Generate random number
  9524.  rem/2                   Remainder of division
  9525.  round/1                 Round to nearest integer
  9526.  truncate/1              Truncate float to integer
  9527.  pi/0                    Mathematical constant
  9528.  
  9529.  sign/1                  Extract sign of value
  9530.  sin/1                   Sine
  9531.  sqrt/1                  Square root
  9532.  tan/1                   Tangent
  9533.  xor/2                   Bitwise exclusive or
  9534.  
  9535.  
  9536. 88..33 OOppeerraattoorrss
  9537.  
  9538.  $                     1    fx   Bind toplevel variable
  9539.  ^                   200   xfy   Predicate
  9540.  ^                   200   xfy   Arithmetic function
  9541.  mod                 300   xfx   Arithmetic function
  9542.  *                   400   yfx   Arithmetic function
  9543.  /                   400   yfx   Arithmetic function
  9544.  //                  400   yfx   Arithmetic function
  9545.  <<                  400   yfx   Arithmetic function
  9546.  
  9547.  >>                  400   yfx   Arithmetic function
  9548.  xor                 400   yfx   Arithmetic function
  9549.  +                   500    fx   Arithmetic function
  9550.  -                   500    fx   Arithmetic function
  9551.  ?                   500    fx   XPCE: obtainer
  9552.  \                   500    fx   Arithmetic function
  9553.  +                   500   yfx   Arithmetic function
  9554.  
  9555.  -                   500   yfx   Arithmetic function
  9556.  /\                  500   yfx   Arithmetic function
  9557.  \/                  500   yfx   Arithmetic function
  9558.  :                   600   xfy   module:term separator
  9559.  <                   700   xfx   Predicate
  9560.  =                   700   xfx   Predicate
  9561.  =..                 700   xfx   Predicate
  9562.  =:=                 700   xfx   Predicate
  9563.  
  9564.  <                   700   xfx   Predicate
  9565.  ==                  700   xfx   Predicate
  9566.  =@=                 700   xfx   Predicate
  9567.  =\=                 700   xfx   Predicate
  9568.  >                   700   xfx   Predicate
  9569.  >=                  700   xfx   Predicate
  9570.  @<                  700   xfx   Predicate
  9571.  
  9572.  @=<                 700   xfx   Predicate
  9573.  @>                  700   xfx   Predicate
  9574.  @>=                 700   xfx   Predicate
  9575.  is                  700   xfx   Predicate
  9576.  \=                  700   xfx   Predicate
  9577.  \==                 700   xfx   Predicate
  9578.  =@=                 700   xfx   Predicate
  9579.  not                 900    fy   Predicate
  9580.  
  9581.  \+                  900    fy   Predicate
  9582.  ,                  1000   xfy   Predicate
  9583.  ->                 1050   xfy   Predicate
  9584.  *->                1050   xfy   Predicate
  9585.  ;                  1100   xfy   Predicate
  9586.  |                  1100   xfy   Predicate
  9587.  discontiguous      1150    fx   Predicate
  9588.  
  9589.  dynamic            1150    fx   Predicate
  9590.  module_transparent 1150    fx   Predicate
  9591.  multifile          1150    fx   Predicate
  9592.  volatile           1150    fx   Predicate
  9593.  initialization     1150    fx   Predicate
  9594.  :-                 1200    fx   Introduces a directive
  9595.  ?-                 1200    fx   Introduces a directive
  9596.  -->                1200   xfx   DCGrammar:  rewrite
  9597.  
  9598.  :-                 1200   xfx   head :- body.  separator
  9599.  
  9600.  
  9601. Bibliography
  9602.  
  9603. [Anjewierden & Wielemaker, 1989]  A.  Anjewierden  and  J.   Wielemaker.
  9604.                                   Extensible  objects.  ESPRIT   Project
  9605.                                   1098 Technical Report  UvA-C1-TR-006a,
  9606.                                   University of Amsterdam, March 1989.
  9607.  
  9608. [BIM, 1989]                       _B_I_M  _P_r_o_l_o_g  _r_e_l_e_a_s_e  _2_._4.   Everberg,
  9609.                                   Belgium, 1989.
  9610.  
  9611. [Bowen & Byrd, 1983]              D. L. Bowen and L. M. Byrd. A portable
  9612.                                   Prolog compiler.  In  L.  M.  Pereira,
  9613.                                   editor,  _P_r_o_c_e_e_d_i_n_g_s   _o_f  _t_h_e   _L_o_g_i_n
  9614.                                   _P_r_o_g_r_a_m_m_i_n_g  _W_o_r_k_s_h_o_p  _1_9_8_3,  Lisabon,
  9615.                                   Portugal, 1983.  Universidade nova  de
  9616.                                   Lisboa.
  9617.  
  9618. [Bratko, 1986]                    I. Bratko. _P_r_o_l_o_g _P_r_o_g_r_a_m_m_i_n_g _f_o_r  _A_r_-
  9619.                                   _t_i_f_i_c_i_a_l _I_n_t_e_l_l_i_g_e_n_c_e. Addison-Wesley,
  9620.                                   Reading, Massachusetts, 1986.
  9621.  
  9622. [Clocksin & Melish, 1987]         W.  F.  Clocksin  and  C.  S.  Melish.
  9623.                                   _P_r_o_g_r_a_m_m_i_n_g   _i_n  _P_r_o_l_o_g.    Springer-
  9624.                                   Verlag, New York,  Third, Revised  and
  9625.                                   Extended edition, 1987.
  9626.  
  9627. [Deransart _e_t _a_l_., 1996]          P.  Deransart,    A.  Ed-Dbali,    and
  9628.                                   L. Cervoni.  _P_r_o_l_o_g_:    _T_h_e  _S_t_a_n_d_a_r_d.
  9629.                                   Springer-Verlag, New York, 1996.
  9630.  
  9631. [Kernighan & Ritchie, 1978]       B. W. Kernighan and D. M. Ritchie. _T_h_e
  9632.                                   _C _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e. Prentice-Hall,
  9633.                                   Englewood Cliffs, New Jersey, 1978.
  9634.  
  9635. [OKeefe, 1990]                    R. A.  OKeefe.  _T_h_e _C_r_a_f_t  _o_f  _P_r_o_l_o_g.
  9636.                                   MIT Press, Massachussetts, 1990.
  9637.  
  9638. [Pereira, 1986]                   F. Pereira.  _C_-_P_r_o_l_o_g  _U_s_e_r_'_s  _M_a_n_u_a_l,
  9639.                                   1986.
  9640.  
  9641. [Qui, 1997]                       _Q_u_i_n_t_u_s   _P_r_o_l_o_g_,   _U_s_e_r   _G_u_i_d_e   _a_n_d
  9642.                                   _R_e_f_e_r_e_n_c_e  _M_a_n_u_a_l.  Berkhamsted,   UK,
  9643.                                   1997.
  9644.  
  9645. [Sterling & Shapiro, 1986]        L.  Sterling  and   E.  Shapiro.   _T_h_e
  9646.                                   _A_r_t _o_f _P_r_o_l_o_g.  MIT Press,  Cambridge,
  9647.                                   Massachusetts, 1986.
  9648.  
  9649. [Warren, 1983]                    D.   H.  D.   Warren.    The   runtime
  9650.                                   environment  for  a  prolog   compiler
  9651.                                   using  a  copy  algorithm.   Technical
  9652.                                   Report 83/052,  SUNY and Stone  Brook,
  9653.                                   New York, 1983.  Major revision  March
  9654.                                   1984.
  9655.  
  9656.                                   722
  9657.